• + 0 comments

    with challenge_data as (

    select  clg.contest_id, 
    
            sum(total_submissions) ts, 
                        sum(total_accepted_submissions) tas
    
    from colleges clg 
    
    join challenges c on clg.college_id = c.college_id
    
    join submission_stats st on st.challenge_id = c.challenge_id
    
    group by clg.contest_id
    

    ),

    views as (

    select  clg.contest_id,
    
            sum(total_views) tv,
    
            sum(total_unique_views) tuv
    
        from view_stats vt 
    
    join challenges c on c.challenge_id = vt.challenge_id
    
    join colleges clg on clg.college_id = c.college_id
    
    group by clg.contest_id
    

    )

    /Main combining select/

    select c.contest_id,

        c.hacker_id, 
    
        c.name, 
    
        cd.ts,
    
        cd.tas,
    
        v.tv,
    
        v.tuv
    

    from contests c join challenge_data cd on cd.contest_id = c.contest_id join views v on v.contest_id = c.contest_id order by c.contest_id