Most gold, silver, bronze badges

1

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow на русском Meta

Вопросы и ответы о сайте для программистов

;  with badges_cte as
(SELECT TOP(##num?200##)
  UserId,
  sum(case when class = 1 then 1 else 0 end) AS gold,
  sum(case when class = 2 then 1 else 0 end) AS silver,
  sum(case when class = 3 then 1 else 0 end) AS bronze,
  count(Id) AS total
FROM Badges
GROUP BY UserId
ORDER BY gold DESC, silver DESC, bronze DESC, total DESC
)

SELECT 
  RANK() OVER(ORDER BY gold DESC, silver DESC, bronze DESC, total DESC),
  UserId AS [User Link], gold, silver, bronze, total
FROM badges_cte  
ORDER BY gold DESC, silver DESC, bronze DESC, total DESC

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results