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
|
1338 Discussions
|
Please Login in order to post a comment
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
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
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.