Q&A for SharePoint enthusiasts
WITH MonthlyVotes AS ( SELECT YEAR(v.CreationDate) AS YearValue, MONTH(v.CreationDate) AS MonthValue, SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS Upvotes, SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS Downvotes FROM Votes v JOIN Posts ON v.PostId = Posts.Id JOIN PostTags ON Posts.Id = PostTags.PostId JOIN Tags ON PostTags.TagId = Tags.Id --WHERE -- Tags.TagName LIKE '%##TagName##%' GROUP BY YEAR(v.CreationDate), MONTH(v.CreationDate) ) SELECT --(YearValue * 100) + MonthValue AS YearMonthNumeric, (YearValue - YEAR(GETDATE())) * 12 + MonthValue AS YearMonthNumeric, Upvotes, Downvotes FROM MonthlyVotes ORDER BY YearValue DESC, MonthValue DESC;