Q&A about the site for mechanics and DIY enthusiast owners of cars, trucks, and motorcycles
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 TagName, CreationYear, CreationMonth from (select TagId, max(CreationYear) as CreationYear, max(CreationMonth) as CreationMonth, max(PostsCount) as MaxPostsCount from tt group by TagId ) AS er left join Tags on er.TagId=Tags.Id order by CreationYear, CreationMonth