PCHomepage
Programmer
I looked through other postings about this but it wasn't clear to me what I needed to do in this case. I have a single row of data, summed from multiple rows, and I am trying to get only the top 5 from those results. How is that done?
Also, is there a way to substitute the column names on the fly, possibly from an array, list or another table, so that I have the name and the top 5 summed values for printing?
Any ideas? Thanks in advance.
Thanks in advance
Also, is there a way to substitute the column names on the fly, possibly from an array, list or another table, so that I have the name and the top 5 summed values for printing?
Any ideas? Thanks in advance.
Code:
USE OSHPD
DECLARE @YearVal varchar(30);
SET @YearVal = 2010;
SELECT SUM([INFECT-DISEASE]),
SUM(NEOPLSMS),
SUM(ENDOCRN),
SUM(BLOOD_DIS),
SUM(MENTAL),
SUM(NERVIOUS),
SUM(CIRCULATORY),
SUM(RESPIRTRY),
SUM([DIGEST]),
SUM(GENOURINRY),
SUM(PREG),
SUM(SKIN),
SUM(MUSCLSKTAL),
SUM(CONGNTL),
SUM(PERNATL),
SUM([ILL-DEFND]),
SUM(POISON),
SUM([HLTH FACTORS]),
SUM([DENTAL-DX]),
SUM([FAM PLN S]),
SUM([OTH-DX]),
SUM(TOTL_DX),
SUM([E&M-NEW]),
SUM([E&M-ESTAB]),
SUM([E&M-HOSP]),
SUM([E&M-CONSLTS]),
SUM([E&M-OTH]),
SUM([E&M-SNF-REL]),
SUM([E&M-CASE MGT]),
SUM([E&M-PRVENT-CHLD]),
SUM([E&M-PRVENT-ADLT]),
SUM([E&M-CONSUL]),
SUM([ANESTHS-PROC]),
SUM([INTGMNTRY-PROC]),
SUM([MUSCLSKTAL-PROC]),
SUM([RESPIR-PROC]),
SUM([CARDIO-PROC]),
SUM([LYMPH-PROC]),
SUM([DIAPH-PROC]),
SUM([DIGEST-PROC]),
SUM([URINARY-PROC]),
SUM([MALE-GNTL-PROC]),
SUM([INTSEX-SURG]),
SUM([FEMALE-GNTL-PROC]),
SUM([MATRNL-CARE]),
SUM([ENDOCRIN-PROC]),
SUM([NERV-PROC]),
SUM([EYE-PROC]),
SUM([AUDITRY-PROC]),
SUM([RADIOLGY-PROC]),
SUM([PATH-PROC]),
SUM([SP SRVS-PROC]),
SUM([Z CODES]),
SUM([DENTAL-PROC]),
SUM([CAT III CODES]),
SUM([OTH-PROC]),
SUM(TOTL_PROC),
SUM(MAMMOGRM),
SUM(HIV),
SUM([PAP SMR]),
SUM([CONTRCPTV MGMT]),
SUM([D-P-T]),
SUM([HIB B]),
SUM([HEP-A]),
SUM([HEP-B]),
SUM([HEP-B_HIB]),
SUM(FLU),
SUM(MMR),
SUM(PNEMOCCAL),
SUM(POLIO),
SUM(VARICELLA)
FROM [Section 1-2] INNER JOIN
[Section 3-5] ON [Section 1-2].OSHPD_ID = [Section 3-5].OSHPD_ID INNER JOIN
[Section 6-8] ON [Section 1-2].OSHPD_ID = [Section 6-8].OSHPD_ID
WHERE [Section 1-2].OSHPD_ID IN (SELECT OSHPD_ID FROM PARTNER_CLINICS)
AND YEAR(END_DATE) = @YearVal
AND [Section 3-5].REPT_YEAR = @YearVal
AND [Section 6-8].REPT_YEAR = @YearVal
Thanks in advance