Sql Combining Several Select Results
I have three SELECT statements that each return a total, 'New Cases', 'Closes Cases', 'Existing Cases'. How do I combine these so they are returned in one resultset. ie I need a t
Solution 1:
As you are counting the same data, you can do it in parallel:
selectsum(casewhen CaseStartDate between'2009-01-01'and'2009-03-31'then1else0end) as [New Cases],
sum(casewhen CaseClosedDate between'2009-01-01'and'2009-03-31'then1else0end) as [Closed Cases],
sum(casewhen CaseStartDate <='2009-03-31'then1else0end) as [Existing Cases]
from
dbo.ClientCase
Solution 2:
@Mitch , @Guffa .. A good solution but in order to get the correct result, you need to take both of CaseStartDate and CaseClosedDate fields into consideration. Eg. in New Cases, if you don't filter CaseClosedDate data you won't get the correct no. of NewCases as it will still count in the closed cases too.
Sorry I have to post it as an answer coz i don't have enuf reputations to add a comment.
Post a Comment for "Sql Combining Several Select Results"