15 Days of Learning SQL

  • + 0 comments
    SELECT A.submission_date, A.unique_hackers, B.top_hacker_id, B.name
    FROM
    
    (SELECT submission_date AS submission_date, SUM(unique_hackers) AS unique_hackers FROM
        (SELECT s1.submission_date AS submission_date, COUNT(DISTINCT s2.hacker_id) AS unique_hackers
        FROM Submissions s1 JOIN Submissions s2
        ON s2.submission_date <= s1.submission_date
        GROUP BY s1.submission_date, s2.hacker_id
        HAVING COUNT(DISTINCT s2.submission_date) = DAY(s1.submission_date)
        ORDER BY s1.submission_date) uk
    GROUP BY submission_date 
    ORDER BY submission_date) A
    
    JOIN
    
    (SELECT t.submission_date AS submission_date, t.top_hacker_id AS top_hacker_id, h.name AS name
    FROM
    (SELECT s3.submission_date AS submission_date, 
        (SELECT hacker_id FROM Submissions s4
         WHERE s4.submission_date = s3.submission_date
         GROUP BY s4.hacker_id
         ORDER BY COUNT(s4.submission_id) DESC, s4.hacker_id 
         LIMIT 1) AS top_hacker_id
     FROM Submissions s3
     GROUP BY s3.submission_date) t 
    JOIN Hackers h
    ON t.top_hacker_id = h.hacker_id
    ORDER BY t.submission_date) B
    
    ON A.submission_date = B.submission_date
    ORDER BY A.submission_date;