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
|
1315 Discussions
|
Please Login in order to post a comment
SELECT c.contest_id, c.hacker_id, c.name, COALESCE(SUM(ss.total_submissions), 0) AS total_submissions, COALESCE(SUM(ss.total_accepted_submissions), 0) AS total_accepted_submissions, COALESCE(SUM(v.total_views), 0) AS total_views, COALESCE(SUM(v.total_unique_views), 0) 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 ( SELECT challenge_id, SUM(total_submissions) AS total_submissions, SUM(total_accepted_submissions) AS total_accepted_submissions FROM Submission_Stats GROUP BY challenge_id ) 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 ) v ON ch.challenge_id = v.challenge_id GROUP BY c.contest_id, c.hacker_id, c.name HAVING SUM(ss.total_submissions) > 0 OR SUM(ss.total_accepted_submissions) > 0 OR SUM(v.total_views) > 0 OR SUM(v.total_unique_views) > 0 ORDER BY c.contest_id;
can anyone fix my query please mssqlserver :( WITH SUM_SUB AS ( SELECT challenge_id, SUM(COALESCE(total_accepted_submissions, 0)) AS total_accepted_submissions, SUM(COALESCE(total_submissions, 0)) AS total_submissions FROM Submission_Stats GROUP BY challenge_id ), SUM_VIEW AS ( SELECT challenge_id, SUM(COALESCE(total_views, 0)) AS total_views, SUM(COALESCE(total_unique_views, 0)) AS total_unique_views FROM View_Stats GROUP BY challenge_id ), CON_VIEW AS ( SELECT
con.contest_id, con.hacker_id, con.name, col.college_id, cal.challenge_id, total_views, total_unique_views FROM Contests con JOIN Colleges col ON con.contest_id = col.contest_id JOIN Challenges cal ON col.college_id = cal.college_id
JOIN SUM_VIEW ss ON cal.challenge_id = ss.challenge_id ), CON_SUB AS ( SELECT
con.contest_id, con.hacker_id, con.name, col.college_id, cal.challenge_id, total_submissions, total_accepted_submissions FROM Contests con JOIN Colleges col ON con.contest_id = col.contest_id JOIN Challenges cal ON col.college_id = cal.college_id LEFT JOIN SUM_SUB ss ON cal.challenge_id = ss.challenge_id ) SELECT con_sub.contest_id, con_sub.hacker_id, con_sub.name, SUM(COALESCE(con_sub.total_submissions, 0)), SUM(COALESCE(con_sub.total_accepted_submissions, 0)), SUM(COALESCE(con_view.total_views, 0)), SUM(COALESCE(con_view.total_unique_views, 0)) FROM con_sub FULL JOIN con_view ON con_sub.contest_id = con_view.contest_id GROUP BY con_sub.contest_id, con_sub.hacker_id, con_sub.name HAVING SUM(COALESCE(con_sub.total_submissions, 0)) + SUM(COALESCE(con_sub.total_accepted_submissions, 0)) + SUM(COALESCE(con_view.total_views, 0)) + SUM(COALESCE(con_view.total_unique_views, 0)) > 0 ORDER BY con_sub.contest_id;
I really didn’t like this question. I wasted so much time trying to validate my query, thinking it was wrong, only to find out it was fine. The real issue was the duplicates in the stats tables (view_stats and submission_stats). It doesn’t make sense why there are duplicates. They should’ve mentioned that in the question. This is a SQL test, not an English test!
my answer - MySQL:
Horrible question.
Logically view_stats rows > submissions_stats rows , as people will view before submit, it makes no sense that some rows in submission_stats is not present in view_stats.
And because of this instead of (contests inner join college inner join challenges ) left join (view_stats left join submission_stats on vs.challenge_id = ss.challenge_id) ,
you'll need to (contests inner join college inner join challenges) left join (view_stats) on contest.challenge_id = vs.challenge_id left join (submission_stats) contest.challenge_id = ss.challenge_id
select * from (select contest_id, hacker_id, name, coalesce(sum(total_submissions),0) as ts, coalesce(sum(total_accepted_submissions),0) as tas, coalesce(sum(total_views),0) as tv, coalesce(sum(total_unique_views),0) as tuv from ((select cts.contest_id, hacker_id, name, chln.challenge_id from contests cts join colleges clg on cts.contest_id = clg.contest_id join challenges chln on clg.college_id = chln.college_id)sqa 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)sqb on sqa.challenge_id = sqb.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)sqc on sqa.challenge_id = sqc.challenge_id) group by contest_id, hacker_id, name)sq1 where ts <> 0 or tas <> 0 or tv <> 0 or tuv <> 0 order by contest_id;
Easy to understand code for MySQL and ORACLE
Here the mysql using its old server, you will recieve Styntax error
MySQL table creation code :
Note : Please run in oracle server