Put empty spaces in an SQL select

Posted by David Undy on Stack Overflow See other posts from Stack Overflow or by David Undy
Published on 2012-09-26T03:19:03Z Indexed on 2012/09/26 3:37 UTC
Read the original article Hit count: 88

Filed under:
|

I'm having difficulty creating a month->count select query in SQL.

Basically, I have a list of entries, all of which have a date associated with them. What I want the end result to be, is a list containing 12 rows (one for each month), and each row would contain the month number (1 for January, 2 for February, etc), and a count of how many entries had that month set as it's date. Something like this:

Month - Count
1     - 12
2     - 0
3     - 7
4     - 0
5     - 9
6     - 0

I can get an result containing months that have a count of higher than 0, but if the month contains no entries, the row isn't created. I get this result just by doing

SELECT Month(goalDate) as monthNumber, count(*) as monthCount 
FROM goalsList 
WHERE Year(goalDate) = 2012 
GROUP BY Month(goalDate) 
ORDER BY monthNumber

Thanks in advance for the help!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql