Q&A for professional and enthusiast programmers
with temp as ( select count(l.PostId) as Posts_num, l.TagId as TagId, YEAR(r.CreationDate) as CreationYear, MONTH(r.CreationDate) as CreationMonth from PostTags as l join PostHistory as r on l.PostId = r.PostId Group by TagId, CreationDate ), tt as ( select sum(Posts_num) as PostsCount, TagId, CreationYear, CreationMonth from temp group by CreationYear, CreationMonth, TagId ) SELECt TagName, CreationYear, CreationMonth from (select TagId, max(CreationYear) as CreationYear, CreationMonth, max(PostsCount) as MaxPostsCount from tt group by TagId, tt.creationmonth ) AS er left join Tags on er.TagId=Tags.Id