I work with SQL Server 2008. There is table with projects, their status and date:
ID Status Date
1 Gone 21-10-2011
2 Cancelled 01-10-2011
3 Won 02-10-2011
4 Won 07-10-2011
5 Gone 02-01-2010
6 Won 24-01-2010
For statistics purposes I need some T-SQL query that in result will return table like this:
Month Year Total Lost Won
10 2011 4 2 2
01 2010 2 1 1
Where:
Total means total entries amount for unique Month-Year pair.
Lost means total entries amount for unique Month-Year pair with status ‘Gone’ or ‘Cancelled’
Won means total entries amount for unique Month-Year pair with status ‘Won’.
I understand how can I get total amount for unique Month-Year, but don’t know how to add in result set Lost and Won columns and get entries amount. And of course if there are no any ‘Won’ projects for Month-Year pair and only ‘Lost’ it should be 0 value for Won column.
This works because the silent ELSE in the CASE gives NULL, and COUNT will ignore this