I have a table that has several date fields. For example, in the defects table, I have the following: dateAssigned, dateCompleted, dateResolved. I am trying to get a query that summarizes the defects as such:
| Number Assigned | Number Completed | Number Resolved
-----------------------------+------------------+-----------------
Mar-2011 | 33 | 22 | 33
Apr-2011 | 10 | 11 | 22
May-2011 | 22 | 66 | 46
etc
I have come up with the following to no avail:
SELECT year(d.dateAssigned)
,month(d.dateAssigned)
,COUNT(d.dateAssigned)
,COUNT(d.dateCompleted)
,COUNT(d.dateResolved)
FROM defect d
GROUP BY year(d.dateAssigned), month(d.dateAssigned)
ORDER BY year(d.dateAssigned), month(d.dateAssigned)
This works correctly for summarizing the dateAssigned defects, but not for the others. I realize this is probably due to the fact I am grouping by dateAssigned, but I dont know how else to do it.
Any help would be appreciated.
This is one way to do it.
The sub select creates two columns typedate and type
Which will look like this
Then I used
SUM(CASE...to pivot the data. If you’re using a RDBMS that supports it you could use pivot insteadThe final output looks something like this
I’ll leave it to you to do the formatting of the year and month