WITH RankedBadges AS ( SELECT UserId, Name, COUNT(*) AS R...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

WITH RankedBadges AS (
  SELECT 
    UserId, Name, COUNT(*) AS Rank, MAX(Date) AS Date
  FROM
    Badges
  GROUP BY UserId, Name
)

SELECT
  Position
FROM (
  SELECT
    UserId, ROW_NUMBER() OVER (ORDER BY Date) AS Position
  FROM
    RankedBadges
  WHERE Name = ##Badge:string##
    AND Rank = ##Rank##
) awarded
WHERE UserId = ##UserId##

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached