-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path15-days-of-learning-sql.sql
34 lines (34 loc) · 1.14 KB
/
15-days-of-learning-sql.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
-- Author: Md. Hasibul Hasan Shovo
-- Email: hasibulhshovo@gmail.com
-- LinkedIn: https://www.linkedin.com/in/iam-hasibul/
WITH
daily_sub AS (
SELECT submission_date,
COUNT(DISTINCT hacker_id) sub_cnt
FROM
(
SELECT submission_date, hacker_id,
DENSE_RANK() OVER (PARTITION BY hacker_id ORDER BY submission_date ASC)
AS hacker_id_dr,
DENSE_RANK() OVER (ORDER BY submission_date ASC) AS DATE_DR
FROM SUBMISSIONS
) WHERE hacker_id_dr = date_dr
GROUP BY submission_date
),
max_sub AS (
SELECT submission_date, hacker_id FROM
(
SELECT submission_date, hacker_id, cnt,
ROW_NUMBER() OVER (PARTITION BY submission_date ORDER BY cnt DESC,
hacker_id ASC) AS row_num FROM
(
SELECT submission_date, hacker_id, COUNT(submission_id) AS cnt
FROM submissions
GROUP BY submission_date, hacker_id
)
) WHERE row_num = 1
)
SELECT ms.submission_date, sub_cnt, ms.hacker_id, h.name
FROM max_sub ms
LEFT JOIN hackers h ON h.hacker_id = ms.hacker_id
LEFT JOIN daily_sub ds ON ms.submission_date = ds.submission_date;