-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path15 Days of Learning SQL.sql
More file actions
22 lines (22 loc) · 1.11 KB
/
15 Days of Learning SQL.sql
File metadata and controls
22 lines (22 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
with cte as(
select submission_date, hacker_id, name from(
select row_number() over (partition by tmp.submission_date order by tmp.dateCount desc, tmp.hacker_id) as rownum, submission_date, hacker_id, name from
(
select s.submission_date, h.hacker_id, h.name, count(h.hacker_id)dateCount
from hackers h
inner join submissions s on (h.hacker_id = s.hacker_id) group by s.submission_date, h.hacker_id, h.name
)tmp
)tmp1 where rownum=1
),
dwc as(
select tmp.submission_date, count(tmp.hacker_id) as hcount from (
select s1.hacker_id, s1.submission_date,count(distinct s2.submission_date) sc, datediff(day, '2016-03-01', s1.submission_date) dd
from submissions s1
inner join submissions s2 on (s1.hacker_id = s2.hacker_id and s1.submission_date >= s2.submission_date)
group by s1.hacker_id, s1.submission_date
) tmp where sc-dd=1
group by tmp.submission_date
)
Select c.submission_date, d.hCount, c.hacker_id, c.name
from cte c
inner join dwc d ON (c.submission_date = d.submission_date);