I have table in SQL Server DB:
ID Open_date Close_date
1 01-01-2010 01-03-2010
2 21-01-2011 12-02-2011
3 01-03-2010 NULL
4 10-01-2010 NULL
I need to make some T-SQL query that will return:
Month Year Open Close
01 2010 2 0
02 2010 0 0
03 2010 0 1
04 2010 0 0
05 2010 0 0
06 2010 0 0
07 2010 0 0
08 2010 0 0
09 2010 0 0
10 2010 0 0
11 2010 0 0
12 2010 0 0
01 2011 1 0
02 2011 0 1
Amount of entries in result set equals amount of months between min value from both 'Open_date' and 'Close_date' columns of table from DB and max value from same columns. The problem is how to find min and max value from two date columns, then generate dates from min to max and then use this temporary table(or what it would be) to count number of open and closed entries for every date from temporary table.