Hi all!I have a select statement I'm trying to create in the most efficient way and the best I have come up with is creating 2 views, both with case statements and selecting on that. I was wondering if someone can give me some ideas on how to make this a little faster. I come up with 45 results and it takes about 10 seconds to run.The first view is:SELECT dbo.TEST.SAMPLE_NUMBER, SUM( CASE analysis WHEN 'test1' THEN CASE test.status WHEN 'i' THEN 1 END WHEN 'test2' THEN CASE test.status WHEN 'i' THEN 1 END ELSE 0 END) AS NeededInCompleteFROM dbo.TEST GROUP BY dbo.TEST.SAMPLE_NUMBER, dbo.TEST.STATUS
The second view is:SELECT dbo.TEST.SAMPLE_NUMBER, SUM( CASE analysis WHEN 'test3' THEN CASE test.status WHEN 'c' THEN 1 END WHEN 'test4' THEN CASE test.status WHEN 'c' THEN 1 END ELSE 0 END) AS NeededCompleteFROM dbo.TEST GROUP BY dbo.TEST.SAMPLE_NUMBER, dbo.TEST.STATUS
And the final select statement: SELECT c.SAMPLE_NUMBERFROM dbo.vw_2 AS c INNER JOINdbo.vw_1 AS i ON c.SAMPLE_NUMBER = i.SAMPLE_NUMBERWHERE (c.NeededComplete > 0) AND (i.NeededInComplete > 0)
This works, but seems like it could be simplified. Any ideas?