Q&A about the site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation
;with tagbadges_cte AS ( SELECT b.UserId AS uid, sum(case when b.class = 1 then 1 else 0 end) AS gold, sum(case when b.class = 2 then 1 else 0 end) AS silver, sum(case when b.class = 3 then 1 else 0 end) AS bronze, count(b.Id) AS total FROM Badges b INNER JOIN Tags t on t.TagName = Name GROUP BY UserId ) SELECT TOP(##num?100##) u.Id As [User Link], u.Reputation, b.gold, b.silver, b.bronze FROM tagbadges_cte b INNER JOIN Users u ON b.uid=u.Id WHERE (gold>=##min?5##) ORDER BY gold DESC,silver DESC,bronze DESC