I have a query which retrieves a list of count grouped by month as below
SELECT CASE WHEN MONTH(StartDate) = 1 THEN 'January' WHEN MONTH(StartDate) = 2 then 'February' WHEN MONTH(StartDate) = 3 then 'March' WHEN MONTH(StartDate) = 4 then 'April' WHEN MONTH(StartDate) = 5 then 'May' WHEN MONTH(StartDate) = 6 then 'June' WHEN MONTH(StartDate) = 7 then 'July' WHEN MONTH(StartDate) = 8 then 'August' WHEN MONTH(StartDate) = 9 then 'September' WHEN MONTH(StartDate) = 10 then 'October' WHEN MONTH(StartDate) = 11 then 'November' WHEN MONTH(StartDate) = 12 then 'December' ELSE '' END AS [month], COUNT(*) AS Count FROM Users WHERE YEAR(StartDate) = '2018' GROUP BY MONTH(StartDate) ORDER BY MONTH(StartDate) The result looks something like this:
Month | Count ---------+------- January | 1 February | 2 April | 7 May | 3 As you can see there are missing months on the list which id like to retrieve. Expected result:
Month | Count ---------+------- January | 1 February | 2 March | 0 April | 7 May | 3 June | 0 Etc...
How can I get the months with zero count?
DATENAME(MONTH, StartDate) AS Monthinstead of that big, unwieldyCASEexpression.....