How many upvotes do I have towards tag-specialist badges?

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

-- Enter Query Title
-- Enter Query Description

DECLARE @UserId int = ##UserId##
;
WITH detail as (
  SELECT
      TagName,
      -- 20/80/200    for bronze/silver/gold badges
      COUNT(distinct Posts.Id) as Posts,
      -- 100/400/1000 for bronze/silver/gold badges
      SUM(case when Votes.VoteTypeId = 2 then 1
               when Votes.VoteTypeId = 3 then -1
          end) AS Score,
      SUM(case when Votes.VoteTypeId = 2 then 1 end) AS Upvotes,
      SUM(case when Votes.VoteTypeId = 3 then 1 end) AS Downvotes,
      SUM(case when Votes.VoteTypeId = 1 then 1 end) AS Accepted
  FROM Tags
      INNER JOIN PostTags ON PostTags.TagId = Tags.id
      INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
      LEFT JOIN Votes ON Votes.PostId = Posts.Id
  WHERE 
      Posts.OwnerUserId = @UserId
      AND Posts.CommunityOwnedDate IS NULL
  GROUP BY TagName 
)
, badges as (
  SELECT 20 as Posts, 100 as Score, 1 as BadgeClass, 'bronze' as BadgeName UNION ALL
  SELECT 80 as Posts, 400 as Score, 2 as BadgeClass, 'silver' as BadgeName UNION ALL
  SELECT 200 as Posts, 1000 as Score, 3 as BadgeClass, 'gold' as BadgeName UNION ALL
  SELECT 10000 as Posts, 10000 as Score, 4 as BadgeClass, 'ultimate' as BadgeName
)
, badge_progress as (
  SELECT detail.*
      , badges.BadgeClass
      , badges.BadgeName
      , row_number() over (partition by tagname order by badgeclass asc) as rn
--    , case when Posts >= 20 and Score >= 100 then 1 end as x 
  FROM detail
  JOIN badges ON detail.Posts < badges.Posts and detail.Score < badges.Score
)
SELECT TOP 50 *
FROM badge_progress
WHERE rn=1
ORDER BY Score DESC

Enter Parameters

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