Q&A about the site for mechanics and DIY enthusiast owners of cars, trucks, and motorcycles
WITH post_temp AS ( SELECT OwnerUserId , COUNT(DISTINCT Id) AS created_posts FROM Posts WHERE CreationDate > DATEADD(year, -5, GETDATE()) GROUP BY OwnerUserId ), comment_temp AS ( SELECT UserID , COUNT(DISTINCT Id) AS created_comments FROM Comments WHERE CreationDate > DATEADD(year, -5, GETDATE()) GROUP BY UserId ) SELECT TOP 10 u.Id , u.DisplayName , (p.created_posts + c.created_comments) AS total FROM post_temp p FULL OUTER JOIN comment_temp c ON c.UserId = p.OwnerUserId INNER JOIN Users u ON u.Id = p.OwnerUserId WHERE u.Id <> -1 ORDER BY total DESC ;