forked from sknsht/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.sql
40 lines (40 loc) · 1.18 KB
/
Solution.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
WITH R(SUBMISSION_DATE, HACKER_ID) AS
(SELECT DISTINCT SUBMISSION_DATE,
HACKER_ID
FROM SUBMISSIONS
WHERE SUBMISSION_DATE = TO_DATE('2016-03-01')
UNION ALL SELECT CHILD.SUBMISSION_DATE,
CHILD.HACKER_ID
FROM R PARENT,
SUBMISSIONS CHILD
WHERE PARENT.SUBMISSION_DATE + 1 = CHILD.SUBMISSION_DATE
AND PARENT.HACKER_ID = CHILD.HACKER_ID),
TOTAL AS
(SELECT SUBMISSION_DATE,
COUNT(DISTINCT HACKER_ID) AS TOTAL
FROM R
GROUP BY SUBMISSION_DATE),
COUNTER AS
(SELECT SUBMISSION_DATE,
HACKER_ID,
COUNT(HACKER_ID) AS N
FROM SUBMISSIONS
GROUP BY SUBMISSION_DATE,
HACKER_ID),
MAXPERDAY AS
(SELECT C.SUBMISSION_DATE,
MIN(C.HACKER_ID) AS HACKER_ID
FROM COUNTER C
WHERE C.N =
(SELECT MAX(K.N)
FROM COUNTER K
WHERE C.SUBMISSION_DATE = K.SUBMISSION_DATE)
GROUP BY SUBMISSION_DATE)
SELECT TOTAL.SUBMISSION_DATE,
TOTAL.TOTAL,
HACKERS.HACKER_ID,
HACKERS.NAME
FROM TOTAL
JOIN MAXPERDAY ON TOTAL.SUBMISSION_DATE = MAXPERDAY.SUBMISSION_DATE
JOIN HACKERS ON MAXPERDAY.HACKER_ID = HACKERS.HACKER_ID
ORDER BY TOTAL.SUBMISSION_DATE;