Vanity: Which users earned a particular tag-based badge sooner than me?

1

Please login or register to vote for this query.

(click on this box to dismiss)

The Great Outdoors

Q&A for people who love being outdoors enjoying nature and wilderness, and learning about the required skills and equipment

-- Which users earned a particular tag-based badge sooner than me?

DECLARE @BadgeName nvarchar(255) = '##BadgeName##'
DECLARE @BadgeType int = ##BadgeType## -- 1: Gold, 2: Silver, 3: Bronze
DECLARE @UserId int = ##UserId##

SELECT
Users.DisplayName,
Users.Id,
DATEDIFF(day, Users.CreationDate, Badges.Date) AS Days
FROM Badges
  INNER JOIN Users ON Users.Id = Badges.UserId
WHERE
  Badges.Class = @BadgeType
  AND Badges.TagBased = 1 -- for true
  AND Badges.Name = @BadgeName
  AND DATEDIFF(day, Users.CreationDate, Badges.Date) <=
    (SELECT DATEDIFF(day, Users.CreationDate, Badges.Date)
    FROM Badges
      INNER JOIN Users ON Users.Id = Badges.UserId
    WHERE
      Badges.Class = @BadgeType
      AND Badges.TagBased = 1 -- for true
      AND Badges.Name = @BadgeName
      AND Badges.UserId = @UserId)
ORDER BY Days Asc

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results