15 Days of Learning SQL

  • + 0 comments

    simple, clean solution:

    SELECT Submission_Date
         , Submission_Count
         , BestHacker_Id
         , (SELECT Name
              FROM Hackers
             WHERE Hacker_Id = BestHacker_Id) Name
      FROM (SELECT Submission_Date
                 , SUM(Submission_Count) Submission_Count
                 , MIN(BestHacker_Id) BestHacker_Id
              FROM (SELECT Hacker_Id
                         , Submission_Date
                         , CASE WHEN MAX(Submission_No) OVER (PARTITION BY Submission_Date) = Submission_No
                             THEN Hacker_Id END BestHacker_Id
                         , CASE WHEN COUNT(*) OVER (PARTITION BY Hacker_Id ORDER BY Submission_Date) + DATE'2016-03-01' - 1 = Submission_Date 
                             THEN 1 ELSE 0 END Submission_Count
                      FROM (SELECT h.Hacker_Id
                                 , s.Submission_Date
                                 , COUNT(*) Submission_No
                              FROM Hackers h
                                 , Submissions s
                             WHERE h.Hacker_Id = s.Hacker_Id
                             GROUP BY h.Hacker_Id
                                 , s.Submission_Date))
             GROUP BY Submission_Date)
     ORDER BY Submission_Date;