SQL


Group and sort by month and year

This will take a datetime2 and group the rows together by month and year and then sort i by their count.

SELECT YEAR(datetime), MONTH(datetime), count(*)
FROM [Database].[dbo].[Table]
GROUP BY YEAR(datetime), MONTH(datetime)
ORDER BY YEAR(datetime) desc, MONTH(datetime) desc