forked from siqichen-usc/LeetCode-SQL-Summary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1225_Report_Contiguous_Dates.sql
54 lines (49 loc) · 1.54 KB
/
1225_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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Solution 1: Subquery, Window Function
WITH tb1 AS (
SELECT fail_date AS date, 'failed' AS s
FROM Failed
UNION
SELECT success_date AS date, 'succeeded' AS s
FROM Succeeded
),
tb2 AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY s ORDER BY date) AS r,
ROW_NUMBER() OVER (ORDER BY date) AS r2
FROM tb1
WHERE Year(date) = 2019
)
-- contiguous dates with same period state share the same r2-r,
-- so in a group with same r2-4, the smallest date is start date and the largest date is end date
SELECT s AS period_state, MIN(date) AS start_date, MAX(date) AS end_date
FROM tb2
GROUP BY r2-r, s
ORDER BY start_date;
-- Solution 2: Join, Subquery
WITH tb1 AS (
-- numbering date with different states in chronological order separately
SELECT f1.fail_date AS date, 'failed' AS s, COUNT(*) AS r
FROM Failed f1
JOIN Failed f2
ON f1.fail_date >= f2.fail_date
GROUP BY f1.fail_date
UNION
SELECT s1.success_date AS date, 'succeeded' AS s, COUNT(*) AS r
FROM Succeeded s1
JOIN Succeeded s2
ON s1.success_date >= s2.success_date
GROUP BY s1.success_date
),
tb2 AS (
SELECT *,
-- numbering date in chronological order
DATEPART(dayofyear, date) AS r2
FROM tb1
WHERE Year(date) = 2019
)
-- contiguous dates with same period state share the same r2-r,
-- so in a group with same r2-4, the smallest date is start date and the largest date is end date
SELECT s AS period_state, MIN(date) AS start_date, MAX(date) AS end_date
FROM tb2
GROUP BY r2-r, s
ORDER BY start_date;