We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Join
- Interviews
- Discussions
Interviews
Interviews
Sort by
recency
|
1340 Discussions
|
Please Login in order to post a comment
With Cte As ( Select Colleges.Contest_id, SUM(cr.Total_unique_views) As SumTotal_unique_views,SUM(cr.Total_views) As SumTotal_views From Challenges As c Join Colleges on Colleges.College_id =c.College_id Cross apply (Select * From View_Stats As vs Where vs.Challenge_id=c.Challenge_id) As Cr Group by Colleges.Contest_id ), Cte1 As ( Select Colleges.Contest_id, SUM(Cr1.Total_submissions) As SumTotal_submissions, SUM(Cr1.Total_accepted_submissions) As SumTotal_accepted_submissions From Challenges As c Join Colleges on Colleges.College_id =c.College_id outer apply (Select * From Submission_Stats As ss Where ss.Challenge_id=c.Challenge_id) As Cr1 Group by Colleges.Contest_id )
Select Contests.Contest_id,Contests.Hacker_id,Contests.Name, IsNull(Cte1.SumTotal_submissions,0) As SumTotal_submissions,IsNull(Cte1.SumTotal_accepted_submissions,0) As SumTotal_accepted_submissions, IsNull(Cte.SumTotal_views,0)As SumTotal_views,IsNull(Cte.SumTotal_unique_views,0) As SumTotal_unique_views from Cte join Cte1 on Cte.Contest_id = Cte1.Contest_id Join Contests on Contests.Contest_id = Cte1.Contest_id Where SumTotal_submissions is not null Or SumTotal_accepted_submissions is not null or SumTotal_views is not null or SumTotal_unique_views is not null order by Contests.Contest_id
MS SQL Solution :
with contest_stat as ( select c.contest_id , (total_submissions) as total_submissions ,(total_accepted_submissions) as total_accepted_submissions ,(total_views) as total_views ,(total_unique_views) as total_unique_views from Contests c left join Colleges cl on c.contest_id = cl.contest_id left join Challenges ch
on cl.college_id = ch.college_id /* left join View_Stats vs on ch.challenge_id = vs.challenge_id left join Submission_Stats ss on ch.challenge_id = ss.challenge_id */ left join ( select challenge_id ,sum(total_views) as total_views ,sum(total_unique_views) as total_unique_views from View_Stats group by
challenge_id ) vin on ch.challenge_id = vin.challenge_id left 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 ) vin1 on ch.challenge_id = vin1.challenge_id ) select c.contest_id ,c.hacker_id ,c.name ,sum(total_submissions) ,sum(total_accepted_submissions) ,sum(total_views) ,sum(total_unique_views) from Contests c inner join contest_stat cs on c.contest_id = cs.contest_id group by c.contest_id ,c.hacker_id ,c.name having sum(total_submissions) + sum(total_accepted_submissions) + sum(total_views) + sum(total_unique_views) > 0 order by c.contest_id
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:
Is this problem not accepting CTE's?
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