forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
report-contiguous-dates.sql
27 lines (26 loc) · 975 Bytes
/
report-contiguous-dates.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
# Time: O(nlogn)
# Space: O(n)
SELECT state AS period_state,
Min(date) AS start_date,
Max(date) AS end_date
FROM (SELECT state,
date,
@rank := CASE
WHEN @prev = state THEN @rank
ELSE @rank + 1
end AS rank,
@prev := state AS prev
FROM (SELECT *
FROM (SELECT fail_date AS date,
"failed" AS state
FROM failed
UNION ALL
SELECT success_date AS date,
"succeeded" AS state
FROM succeeded) a
WHERE date BETWEEN '2019-01-01' AND '2019-12-31'
ORDER BY date ASC) b,
(SELECT @rank := 0,
@prev := "unknown") c) d
GROUP BY d.rank
ORDER BY start_date ASC