larrydavid
Programmer
Hello,
Right now, in SS2005, to get the Top 10 number of occurences of codes that span 3 columns, I am using a dervied table with an outer and inner join, the inner join being a union:
Is there a better way to do this? Right now the query is taking just under 2 minutes to run. I'd like to get it down to under 30 seconds. Any suggestions greatly appreciated.
Thanks,
Larry
Right now, in SS2005, to get the Top 10 number of occurences of codes that span 3 columns, I am using a dervied table with an outer and inner join, the inner join being a union:
Code:
SELECT TOP 10 [TEST_CODES], [CODES_count] FROM
(
SELECT TOP 10 [TEST_CODES] = CASE WHEN t.CODE1 = '' THEN 'Blank' ELSE t.CODE1 END,
[CODES_count] = COUNT(*)
FROM TEST t
GROUP BY t.[CODE1]
union all
SELECT TOP 10 CASE WHEN t.CODE2 = '' THEN 'Blank' ELSE t.CODE2 END,
COUNT(*) AS CODE2_count
FROM TEST t
GROUP BY t.[CODE2]
union all
SELECT TOP 10 CASE WHEN t.CODE3= '' THEN 'Blank' ELSE t.CODE3 END,
COUNT(*) AS CODE3_count
FROM TEST T
GROUP BY t.[CODE3]
)x
ORDER BY [CODES_count] DESC
Is there a better way to do this? Right now the query is taking just under 2 minutes to run. I'd like to get it down to under 30 seconds. Any suggestions greatly appreciated.
Thanks,
Larry