15 Days of Learning SQL

  • + 0 comments
    with 
        cte_01 as
    (
        select 
            submission_date,
            hacker_id,
            count(*) no_of_submissions,
            dense_rank() over (order by submission_date) day_count
        from
            Submissions
        group by 
            submission_date,
            hacker_id    
    ),
        cte_02 as
    (
        select 
            *,
            count(*) over (partition by hacker_id order by submission_date) till_date_submissions,
            case when day_count = count(*) over (partition by hacker_id order by submission_date) then 1 else 0 end as unique_flag
        from cte_01
    ),
        cte_03 as
    (
        select
            *,
            sum(unique_flag) over (partition by submission_date) unique_count,
            row_number() over (partition by submission_date order by no_of_submissions desc, hacker_id) rn
        from cte_02
    ),
        cte_04 as
    (
    select * from cte_03
    where rn = 1
    )
    select 
        ct.submission_date,
        ct.unique_count,
        ct.hacker_id,
        hck.name
    from
        cte_04 ct
    left join Hackers hck on ct.hacker_id = hck.hacker_id
    order by
        ct.submission_date,
        ct.hacker_id;