15 Days of Learning SQL

  • + 0 comments
    with cte as (
    select submission_date,hacker_id,count(*) as day_cnt from Submissions group by submission_date,hacker_id 
        ),
    cte2 as (
        select submission_date,hacker_id,count(hacker_id) over (partition by hacker_id order by submission_date asc rows between unbounded preceding and current row) cnt, datediff(day,'2016-02-29',submission_date) diff from cte
    ),
    first_part as (
        select submission_date,count(hacker_id) cnt_dis from cte2 where cnt = diff
        group by submission_date
    ),
    cte3 as (
        select submission_date,c.hacker_id,day_cnt,name,row_number() over (partition by submission_date order by day_cnt desc, c.hacker_id asc ) rw_num from 
        cte c join Hackers h on  c.hacker_id = h.hacker_id
    )
    select f.submission_date,f.cnt_dis,c.hacker_id,c.name from first_part f join cte3 c
    on f.submission_date = c.submission_date
    where rw_num = 1
    order by submission_date``