Skip to content

Commit ef75ace

Browse files
authored
Create report-contiguous-dates.sql
1 parent 887ce32 commit ef75ace

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

MySQL/report-contiguous-dates.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
SELECT state AS period_state,
5+
Min(date) AS start_date,
6+
Max(date) AS end_date
7+
FROM (SELECT state,
8+
date,
9+
@rank := CASE
10+
WHEN @prev = state THEN @rank
11+
ELSE @rank + 1
12+
end AS rank,
13+
@prev := state AS prev
14+
FROM (SELECT *
15+
FROM (SELECT fail_date AS date,
16+
"failed" AS state
17+
FROM failed
18+
UNION ALL
19+
SELECT success_date AS date,
20+
"succeeded" AS state
21+
FROM succeeded) a
22+
WHERE date BETWEEN '2019-01-01' AND '2019-12-31'
23+
ORDER BY date ASC) b,
24+
(SELECT @rank := 0,
25+
@prev := "unknown") c) d
26+
GROUP BY d.rank
27+
ORDER BY start_date ASC

0 commit comments

Comments
 (0)