What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 129 Stack Exchange communities.

Jeff's query for Legendary badge is given here:

http://meta.stackexchange.com/questions/57232/is-150-rep-cap-exceeded-alone-enough-for-legendary-badge/58192#58192

Given that the tables in data.se != tables in production-db, what is the correct translation of this query? Are bounties included as well in "targetrepchange", in particular does offered bounties mean you need to recover that to end the (UTC-based) day with rep-change(day) > 200?

Somewhat related to: Actual badge queries used by StackExchange for use on SEDE


Query reproduced

SELECT u.id 
FROM   users u WITH (nolock), 
       /* return all userids who have hit the rep cap for 150 days */ 
       (SELECT targetuserid AS userid 
        FROM   (SELECT targetuserid, 
                       CONVERT(CHAR(8), creationdate, 112) AS dayperiod, 
                       SUM(targetrepchange)                AS dayrep 
                FROM   posts2votes 
                WHERE  targetrepchange IS NOT NULL 
                       AND deletiondate IS NULL 
                GROUP  BY targetuserid, 
                          CONVERT(CHAR(8), creationdate, 112) 
                HAVING SUM(targetrepchange) >= 200) AS x 
        GROUP  BY targetuserid 
        HAVING COUNT(*) >= 150) x 
WHERE  u.id NOT IN /* return all userids who do NOT have the badge */ 
              (SELECT userid 
               FROM users2badges u2b WITH (nolock) 
               INNER JOIN badges b 
                ON u2b.badgeid = b.id 
               WHERE b.name = 'Legendary' 
              ) 
       AND u.id = x.userid
share|improve this question

1 Answer 1

Here's my attempt

http://data.stackexchange.com/stackoverflow/s/1004/the-real-legendary-badge-query

Note - There is a discrepancy against the real query, the -1s (downvote) are missing from SEDE

Reproduced below

-- the real legendary badge query
-- according to http://meta.stackexchange.com/q/58192

;with posts2votes as
(
select
  p.owneruserid targetuserid,
  v.creationdate,
  case votetypeid
      when 1 then 15
      when 8 then -bountyamount
      when 9 then bountyamount
      when 2 then 10
      when 3 then -2
      end targetrepchange
from votes v
inner join posts p on p.id = v.postid
inner join users u on u.id = p.owneruserid
where v.votetypeid in (1,2,3,8,9)
  and p.communityowneddate is null
  and u.reputation >= 1000 -- quick pre-filter

union all

select
  p2.owneruserid,
  v.creationdate,
  2
from votes v
inner join posts p1 on p1.id = v.postid
  and v.votetypeid = 1 -- question accepted by asker
inner join posts p2 on p2.id = p1.parentid
inner join users u on u.id = p2.owneruserid
where p2.communityowneddate is null
  and u.reputation >= 1000 -- quick pre-filter
)
-- above this line the CTE attempts to rebuild the posts2votes view/table

SELECT u.id as [User Link], u.id
FROM   users u WITH (nolock),
       /* return all userids who have hit the rep cap for 150 days */
       (SELECT targetuserid AS userid
        FROM   (SELECT targetuserid,
                       CONVERT(CHAR(8), creationdate, 112) AS dayperiod,
                       SUM(targetrepchange)                AS dayrep
                FROM   posts2votes
                WHERE  targetrepchange IS NOT NULL
                       --AND deletiondate IS NULL (deleted data not in data.se)
                GROUP  BY targetuserid,
                          CONVERT(CHAR(8), creationdate, 112)
                HAVING SUM(targetrepchange) >= 200) AS x
        GROUP  BY targetuserid
        HAVING COUNT(*) >= 150) x
WHERE  u.id NOT IN /* return all userids who do NOT have the badge */
              (SELECT userid
               FROM badges b
               WHERE b.name = 'Legendary'
              )
       AND u.id = x.userid
share|improve this answer
    
This looks pretty good. Since it was written, question/answer votes also need to be broken out, and suggested edit data is not available in SEDE yet. –  Jon Seigel Jun 23 '11 at 0:08

You must log in to answer this question.

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