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
|
1426 Discussions
|
Please Login in order to post a comment
this is my version:
SET NOCOUNT ON;
WITH SUM_TOTAL_SUBMISSIONS AS ( select a.contest_id, a.hacker_id, a.name, sum(e.total_submissions) AS total_submissions, sum(e.total_accepted_submissions) AS total_accepted_submissions From contests A LEFT JOIN COLLEGES B ON A.contest_id = B.contest_id LEFT JOIN CHALLENGES C ON B.COLLEGE_ID = C.COLLEGE_ID LEFT JOIN SUBMISSION_STATS E ON c.CHALLENGE_ID = E.CHALLENGE_ID GROUP BY a.contest_id, a.hacker_id, a.name ) ,SUM_TOTAL_VIEW as( select a.contest_id, a.hacker_id, sum(d.total_views) AS total_views, sum(d.total_unique_views) AS total_unique_views From contests A LEFT JOIN COLLEGES B ON A.contest_id = B.contest_id LEFT JOIN CHALLENGES 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 ) ,DATA_FINAL AS( SELECT A.*, B.total_views, B.total_unique_views FROM SUM_TOTAL_SUBMISSIONS A INNER JOIN SUM_TOTAL_VIEW B ON A.contest_id = B.contest_id AND A.hacker_id = B.hacker_id )
SELECt * FROM DATA_FINAL WHERE total_submissions > 0 OR total_unique_views > 0 OR total_accepted_submissions > 0 OR total_views > 0 ORDER BY contest_id
go
go
SELECT c.contest_id, c.hacker_id, c.name, IFNULL(s.ts, 0) AS total_submissions, IFNULL(s.tas, 0) AS total_accepted_submissions, IFNULL(v.tv, 0) AS total_views, IFNULL(v.tuv, 0) AS total_unique_views FROM Contests c LEFT JOIN ( SELECT c.contest_id, SUM(ss.total_submissions) AS ts, SUM(ss.total_accepted_submissions) AS tas FROM Contests c JOIN Colleges co ON co.contest_id = c.contest_id JOIN Challenges ch ON ch.college_id = co.college_id JOIN Submission_Stats ss ON ss.challenge_id = ch.challenge_id GROUP BY c.contest_id ) s ON s.contest_id = c.contest_id LEFT JOIN ( SELECT c.contest_id, SUM(vs.total_views) AS tv, SUM(vs.total_unique_views) AS tuv FROM Contests c JOIN Colleges co ON co.contest_id = c.contest_id JOIN Challenges ch ON ch.college_id = co.college_id JOIN View_Stats vs ON vs.challenge_id = ch.challenge_id GROUP BY c.contest_id ) v ON v.contest_id = c.contest_id WHERE (IFNULL(s.ts, 0) + IFNULL(s.tas, 0) + IFNULL(v.tv, 0) + IFNULL(v.tuv, 0)) > 0 ORDER BY c.contest_id;
Why the expected output of the Problem is not provided? I am still not able to understand what is wrong in the below query??
SELECT cs.contest_id ,cs.hacker_id ,cs.name ,sum(nvl(ss.total_submissions,0)) as total_submissions ,sum(nvl(ss.total_accepted_submissions,0)) as total_accepted_submissions ,sum(nvl(vs.total_views,0)) as total_views ,sum(nvl(vs.total_unique_views,0)) as total_unique_views FROM Contests cs LEFT OUTER JOIN Colleges co on cs.contest_id = co.contest_id LEFT OUTER JOIN Challenges ch on co.college_id = ch.college_id LEFT OUTER JOIN Submission_Stats ss on ch.challenge_id = ss.challenge_id LEFT OUTER JOIN View_Stats vs on ch.challenge_id = vs.challenge_id group by cs.contest_id, cs.hacker_id, cs.name having (sum(nvl(ss.total_submissions,0)) + sum(nvl(ss.total_accepted_submissions,0)) + sum(nvl(vs.total_views,0)) + sum(nvl(vs.total_unique_views,0)) ) > 0 order by cs.contest_id;