Report Contiguous Dates Problem


Description

LeetCode Problem 1225.

Table: Failed

1
2
3
4
5
6
7
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| fail_date    | date    |
+--------------+---------+
Primary key for this table is fail_date.
Failed table contains the days of failed tasks.

Table: Succeeded

1
2
3
4
5
6
7
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| success_date | date    |
+--------------+---------+
Primary key for this table is success_date.
Succeeded table contains the days of succeeded tasks.

A system is running one task every day. Every task is independent of the previous tasks. The tasks can fail or succeed.

Write an SQL query to generate a report of period_state for each continuous interval of days in the period from 2019-01-01 to 2019-12-31.

period_state is ‘failed’ if tasks in this interval failed or ‘succeeded’ if tasks in this interval succeeded. Interval of days are retrieved as start_date and end_date.

Order result by start_date.

The query result format is in the following example:

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
Failed table:
+-------------------+
| fail_date         |
+-------------------+
| 2018-12-28        |
| 2018-12-29        |
| 2019-01-04        |
| 2019-01-05        |
+-------------------+

Succeeded table:
+-------------------+
| success_date      |
+-------------------+
| 2018-12-30        |
| 2018-12-31        |
| 2019-01-01        |
| 2019-01-02        |
| 2019-01-03        |
| 2019-01-06        |
+-------------------+


Result table:
+--------------+--------------+--------------+
| period_state | start_date   | end_date     |
+--------------+--------------+--------------+
| succeeded    | 2019-01-01   | 2019-01-03   |
| failed       | 2019-01-04   | 2019-01-05   |
| succeeded    | 2019-01-06   | 2019-01-06   |
+--------------+--------------+--------------+

The report ignored the system state in 2018 as we care about the system in the period 2019-01-01 to 2019-12-31.
From 2019-01-01 to 2019-01-03 all tasks succeeded and the system state was "succeeded".
From 2019-01-04 to 2019-01-05 all tasks failed and system state was "failed".
From 2019-01-06 to 2019-01-06 all tasks succeeded and system state was "succeeded".


MySQL Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
select 'succeeded' as period_state, min(success_date) as start_date, 
    max(success_date) as end_date 
from (select success_date, row_number() over(order by success_date) as row_num
    from Succeeded
    where success_date between '2019-01-01' and '2019-12-31') t
group by dayofyear(success_date) - row_num

union

select 'failed' as period_state, min(fail_date) as start_date, 
    max(fail_date) as end_date 
from (select fail_date, row_number() over(order by fail_date) as row_num
    from Failed
    where fail_date between '2019-01-01' and '2019-12-31') t
group by dayofyear(fail_date) - row_num

order by start_date




Related Posts

Report Contiguous Dates Problem

LeetCode 1225. Write an SQL query to generate a report...

Monthly Transactions II Problem

LeetCode 1205. Write an SQL query to find for each...

Team Scores In Football Tournament Problem

LeetCode 1212. Write an SQL query that selects the team_id,...

Last Person To Fit In The Elevator Problem

LeetCode 1204. Write an SQL query to find the person_name...

Queries Quality And Percentage Problem

LeetCode 1211. Write an SQL query to find each query_name,...

Number Of Comments Per Post Problem

LeetCode 1241. Write an SQL query to find number of...