You are viewing a single comment's thread. Return to all comments →
WITH cte1 AS ( SELECT s.submission_date, s.submission_id, s.hacker_id, h.name , DAY(s.submission_date) AS day_num , DENSE_RANK() OVER(PARTITION BY s.hacker_id ORDER BY s.submission_date) AS rnk FROM submissions s JOIN hackers h ON h.hacker_id = s.hacker_id WHERE s.submission_date BETWEEN '2016-03-01' AND '2016-03-15' ) , cte2 AS ( SELECT submission_date, COUNT(DISTINCT hacker_id) AS unique_h_count FROM cte1 WHERE day_num = rnk GROUP BY submission_date ) , cte3 AS ( SELECT submission_date, hacker_id, COUNT(submission_id) AS s_count FROM cte1 GROUP BY submission_date, hacker_id ) , cte4 AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY submission_date ORDER BY s_count DESC, hacker_id) AS rn FROM cte3 ) SELECT cte4.submission_date, cte2.unique_h_count, cte4.hacker_id, h.name FROM cte4 JOIN hackers h ON h.hacker_id = cte4.hacker_id JOIN cte2 ON cte2.submission_date = cte4.submission_date WHERE rn = 1
Seems like cookies are disabled on this browser, please enable them to open this website
15 Days of Learning SQL
You are viewing a single comment's thread. Return to all comments →