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 TargetPosts, 100 as TargetScore, 1 as BadgeClass, 'bronze' as BadgeName UNION ALL
  SELECT 80, 400, 2, 'silver' UNION ALL
  SELECT 200, 1000, 3, 'gold' UNION ALL
  SELECT 10000, 10000, 4, 'ultimate'
)
, badge_progress as (
  SELECT *
      , row_number() over (partition by tagname order by badgeclass asc) as rn
      , detail.Posts/badges.TargetPosts as Post_progress
      , detail.Score/badges.TargetScore as Score_progress
  FROM detail
  JOIN badges ON detail.Posts < badges.TargetPosts or detail.Score < badges.TargetScore
)
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