18

Just out of curiosity, is there a way to do so? While the badges page shows how rare each badge is, it's interesting to see how rare are certain combinations. Like 3+ "Steward"s or Unsung Hero + Constituent or whatever.

3 Answers 3

9

If you want to select users with specific number of several badges you can use a pivot query to get counts per badge per user and then restrict the rows with a where clause.

This query does that for users with 4 Steward badges and 1 Unsung Hero:

select userid as [User Link]
     , reputation
     , [Steward]      -- which badges you want
     , [Unsung Hero]
from
(
  select userid         -- userid is our group
       , [Steward]      -- which badges you want
       , [Unsung Hero]
  from (
    select id           -- where we count(id) over 
         , userid       -- group over value 
         , name         -- the values will become a column
    from badges
    where tagbased = 0  -- non-tag badges only
  ) as data
  pivot 
  (
    count(data.id)       -- count the number of badges per user per badge
    for data.name in (   -- the badge names come out of this
      [Steward],         -- which badge name you want as column
      [Unsung Hero]
    ) 
  ) pvt
  where [Steward]     = 4  -- criteria for each badge
  and   [Unsung Hero] = 1
) main 
inner join users u on u.id = main.userid -- for reputation
order by displayname

When run today, this will be the result:

enter image description here

To have other badges or other criteria adapt the query by adding the column names that represent a badge name and the where clause accordingly.

I'm leaving the rest of this answer here as well as it has some useful bits, although it doesn't exactly answer the question

I felt this was an opportunity to abuse the PIVOT statement in SQL Server so I created this query.

It calculates a ratio for rare badges and then lists the user with best ratio of rarest badges and shows in the PIVOT table the top 20 of rarest badges (I had to limit this due to the limited time a query can run on SEDE).

This is what the query look like:

with badgerank as 
-- per badge, how often does it occur
(select row_number() over (order by count(*) desc) as rank
     , name
     , count(*) as cnt
from badges
where tagbased = 0
group by name
)
-- calculate a ratio, higher is more rare
, badgerare as
(
    select cast(rank as decimal)/(select count(*) from badgerank) as ratio
    , name
    from  badgerank
)
-- per user, badge_name and its count
, userbadge as
(
  select userid
       , name
       , count(*) as [badge_cnt_user]
  from badges
  where tagbased = 0 
  group by userid, name
)
-- join users and badge to get ratio and badgecount
, userswithbadges as
(
   select b.name
        , b.ratio
        , u.userid
        , u.badge_cnt_user
   from badgerare b
   inner join userbadge u on b.name = u.name
)
-- union to get all data needed for the pivot table
, bdata as 
(
    select badge_cnt_user as id
         , name
         , userid
   from userswithbadges
   union 
   select count(*)
        , 'Count'
        , userid
   from userswithbadges
   group by userid
   union 
   select sum(ratio)
        , 'Ratio'
        , userid
   from userswithbadges
   group by userid
)

select top 1000 userid as [User Link],
     [Count],[Ratio],[Sheriff],[Illuminator],[Legendary],[Research Assistant],[Reversal],[Documentation Beta],[Not a Robot],[Epic],[Publicist],[Synonymizer],[Generalist],[Outspoken],[Refiner],[Archaeologist],[Convention],[Documentation Pioneer],[Marshal],[Sportsmanship],[Beta],[Copy Editor]
from (
select userid
     , name
     , id
from bdata
) as data
pivot
(
sum(id)
for name in ([Count],[Ratio],[Sheriff],[Illuminator],[Legendary],[Research Assistant],[Reversal],[Documentation Beta],[Not a Robot],[Epic],[Publicist],[Synonymizer],[Generalist],[Outspoken],[Refiner],[Archaeologist],[Convention],[Documentation Pioneer],[Marshal],[Sportsmanship],[Beta],[Copy Editor])
) as pvt
order by 3 desc


/*
to get the pivot columns run this:

declare @pivotcols nvarchar(max) =N'[Count],[Ratio]'

select @pivotcols = @pivotcols + ',[' +  name + ']'
from (
  select row_number() over (order by count(*) desc) as rank
       , name
  from badges
  where tagbased = 0
  group by name
) as data
order by rank desc
print @pivotcols
*/

When run today this the outcome:

pivot table users vs badges

It doesn't really show which combination of badges is rare but it feels that question is more suited for a statistical analysis package then a SQL query. Or I'm not that good with SQL after all.

2
  • Cool, thanks) The only missing thing is I wanted "at least 4 Stewards" but substituting where [Steward] = 4 with where [Steward] >= 4 works as expected
    – YakovL
    Feb 1, 2018 at 9:02
  • Yep, that is correct, you can now use simple boolean logic in the where clause for each badge.
    – rene
    Feb 1, 2018 at 9:04
19

You can do this with the Data Explorer. As an example, see the Users eligible for 2011 Stack Overflow moderator elections query:

-- Users eligible for 2011 Stack Overflow moderator elections
-- All users which have the necessary reputation and badges for the 2011 Stack Overflow Moderator election taking part at the end of the year.

Select Id, DisplayName, Reputation
 from Users
 where Reputation > 3000
   and exists (Select Id from Badges b where b.UserId = Users.Id and b.Name = 'Civic Duty')
   and exists (Select Id from Badges b where b.UserId = Users.Id and b.Name = 'Strunk & White')
   and exists (Select Id from Badges b where b.UserId = Users.Id and b.Name = 'Deputy')
   and exists (Select Id from Badges b where b.UserId = Users.Id and b.Name = 'Convention')
   and exists (Select Id from Badges b where b.UserId = Users.Id and b.Name = 'Yearling')
order by DisplayName asc
5
  • 12
    Or with fewer subqueries data.stackexchange.com/stackoverflow/query/794071/… Jan 29, 2018 at 22:14
  • Could you please suggest a query for users having mulptiple badges with the same name? Obviously, adding one more line with and exists and the same badge name won't work, but last time I dealt with SQL was many years ago, so it's not that simple for me. I've tried to modify Martin's query like this data.stackexchange.com/stackoverflow/query/edit/795208 and that worked for "4 Steward badges" but obviously "4 Steward badges and 1 Unsung Hero" are not searched properly – this also counts "5 Steward badges" guys
    – YakovL
    Jan 31, 2018 at 16:07
  • @YakovL I think I finally understood what you need so I edited my answer to add what you describe in your comment. Please check if that matches your needs. (sorry Stijn)
    – rene
    Jan 31, 2018 at 21:48
  • @YakovL Sounds like it's time for you to learn SQL. =)
    – jpmc26
    Jan 31, 2018 at 22:28
  • @rene I don't mind :)
    – user247702
    Feb 1, 2018 at 0:09
1

Sure. You can author a SEDE query to extract the data you want.

Here's a simple query which shows users with the "Good Answer" badge. You'll have you adapt the SQL to fit what you're exactly looking for, though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .