Q&A for professional and enthusiast programmers
with t 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 t group by CreationYear, CreationMonth, TagId ) select TagId, max(CreationYear) as CreationYear, max(CreationMonth) as CreationMonth, max(PostsCount) as MaxPostsCount from tt group by TagId order by TagId