Sort by

recency

|

1338 Discussions

|

  • + 0 comments

    This problem seemed simple at first but it really makes you think hard about how you're using your JOINs like what would happen if you FULL JOIN 2 tables with different number of rows for their common ID column. Also a good case for using COALESCE.

    Answer:

    WITH st AS (
        SELECT col.contest_id
        ,SUM(st.total_views) total_views
        ,SUM(st.total_unique_views) total_unique_views
        ,SUM(st.total_submissions) total_submissions
        ,SUM(st.total_accepted_submissions) total_accepted_submissions
        FROM colleges col
        INNER JOIN (
            SELECT college_id
                ,SUM(st.total_views) total_views
                ,SUM(st.total_unique_views) total_unique_views
                ,SUM(st.total_submissions) total_submissions
                ,SUM(st.total_accepted_submissions) total_accepted_submissions
            FROM challenges cha
            INNER JOIN (
                SELECT COALESCE(v.challenge_id, s.challenge_id) AS challenge_id
                ,COALESCE(
                    total_views
                    ,0
                ) AS total_views
                ,COALESCE(
                    total_unique_views
                    ,0
                ) AS total_unique_views
                ,COALESCE(
                    total_submissions
                    ,0
                ) AS total_submissions
                ,COALESCE(
                    total_accepted_submissions
                    ,0
                ) AS total_accepted_submissions
                FROM (
                    SELECT challenge_id
                    ,SUM(total_views) AS total_views
                    ,SUM(total_unique_views) AS total_unique_views
                    FROM view_stats v
                    GROUP BY challenge_id
                ) v
                FULL OUTER JOIN (
                    SELECT challenge_id
                    ,SUM(total_submissions) AS total_submissions
                    ,SUM(total_accepted_submissions) AS total_accepted_submissions
                    FROM submission_stats
                    GROUP BY challenge_id
                ) s
                ON v.challenge_id = s.challenge_id
            ) st
            ON cha.challenge_id = st.challenge_id
            GROUP BY college_id
        ) st
        ON col.college_id = st.college_id
        GROUP BY col.contest_id
    )
    SELECT con.contest_id
    ,con.hacker_id
    ,con.name
    ,st.total_submissions
    ,st.total_accepted_submissions
    ,st.total_views
    ,total_unique_views
    FROM contests con
    INNER JOIN st
    ON con.contest_id = st.contest_id
    ORDER BY con.contest_id
    
  • + 0 comments

    Is this problem not accepting CTE's?

  • + 1 comment

    The problem here is: There are more than 2 aggreations tables (The view table and the submission table). Therefore, we cannot join all of them together at once because JOIN clause will make the value duplicate resulting in wrong calculation. For example: in for challenge_id 145: There are 4 rows consist 145 in View_Stats table, but there are only 1 row consists 145 in Submission_Stats. When we join all of them at once, the row in submssion table will be quadrupped resulting in wrong sum for total_submissions. So we must aggregate each table separately before joinning all of them together. WITH Views_table AS (SELECT ch.challenge_id, SUM(Total_views) Totals_views, SUM(Total_unique_views) Total_unique_views FROM Challenges ch JOIN View_stats vw ON ch.challenge_id = vw.challenge_id GROUP BY ch.challenge_id), Submissions_table AS (SELECT ch.challenge_id, SUM(Total_submissions) Total_submissions, SUM(Total_accepted_submissions) Total_accepted_submissions FROM Challenges ch JOIN Submission_Stats ss ON ch.challenge_id = ss.challenge_id GROUP BY ch.challenge_id) SELECT ct.contest_id, hacker_id, name, SUM(Total_submissions), SUM(Total_accepted_submissions), SUM(Totals_views), SUM(Total_unique_views) FROM contests ct JOIN colleges cl ON ct.Contest_id = cl.contest_id JOIN challenges ch ON ch.college_id = cl.college_id LEFT JOIN Views_table v ON ch.challenge_id = v.challenge_id LEFT JOIN Submissions_table s ON ch.challenge_id = s.challenge_id GROUP BY ct.contest_id, hacker_id, name ORDER BY ct.contest_id

  • + 0 comments

    select a.contest_id, a.hacker_id,a.name,b.totsubs,b.totaccptd,a.totalviews,a.uniqueviews from (select a.contest_id, a.hacker_id,a. name,sum(d.total_views) as totalviews,sum(d.total_unique_views) as uniqueviews from Contests a join Colleges b on a.contest_id=b.contest_id left join Challenges2 c on b.college_id=c.college_id left join View_Stats d on c.challenge_id=d.challenge_id group by a.contest_id,a.hacker_id,a. name )a join (select a.contest_id, a.hacker_id,a. name,sum(e.total_submissions)totsubs,sum(e.total_accepted_submissions)as totaccptd from Contests a join Colleges b on a.contest_id=b.contest_id left join Challenges2 c on b.college_id=c.college_id right join Submission_Stats e on c.challenge_id=e.challenge_id group by a.contest_id,a.hacker_id,a. name) b on a.contest_id=b.contest_id

  • + 0 comments

    ANOTHER BUG or FAILED PROBLEM DESCRIPTION

    This problem also is not accepting use of CTE. There is a bug or it needs to be clearly displayed that its not to be used.