LeetCode:Database 71.报告系统状态的连续日期

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 71.报告系统状态的连续日期相关的知识,希望对你有一定的参考价值。

要求:编写一个 SQL 查询 2019-01-01 到 2019-12-31 期间任务连续同状态 period_state 的起止日期(start_date 和 end_date)。

系统 每天 运行一个任务。每个任务都独立于先前的任务。任务的状态可以是失败或是成功,如果任务失败了,就是失败状态的起止日期,如果任务成功了,就是成功状态的起止日期,最后结果按照起始日期 start_date 排序。

Table: Failed的结构

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| fail_date    | date    |
+--------------+---------+
该表主键为 fail_date。
该表包含失败任务的天数.

Table: Succeeded的结构

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| success_date | date    |
+--------------+---------+
该表主键为 success_date。
该表包含成功任务的天数.

Failed 表:

+-------------------+
| fail_date         |
+-------------------+
| 2018-12-28        |
| 2018-12-29        |
| 2019-01-04        |
| 2019-01-05        |
+-------------------+

Succeeded 表:

+-------------------+
| 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   |
+--------------+--------------+--------------+
结果忽略了 2018 年的记录,因为我们只关心从 2019-01-01 到 2019-12-31 的记录
从 2019-01-01 到 2019-01-03 所有任务成功,系统状态为 "succeeded"。
从 2019-01-04 到 2019-01-05 所有任务失败,系统状态为 "failed"。
从 2019-01-06 到 2019-01-06 所有任务成功,系统状态为 "succeeded"。

SQL语句:

#1.方法1
SELECT * FROM(
SELECT 'failed'  AS period_state,MIN(fail_date) AS start_date ,MAX(fail_date) AS end_date
FROM(
SELECT fail_date,ADDDATE(fail_date,-row_number() over(ORDER BY fail_date ASC)) AS r
FROM failed
WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31'
)a
GROUP BY r

UNION ALL

SELECT 'succeeded'  AS period_state,MIN(success_date)AS start_date,MAX(success_date)AS end_date
FROM(
SELECT success_date,ADDDATE(success_date,-row_number() over(ORDER BY success_date ASC)) AS r
FROM succeeded
WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31'
)a
GROUP BY r)b
ORDER BY start_date ASC;
#2.方法2
select  period_state,min(d) as start_date,max(d) as end_date
from(
select 'failed'  AS period_state,fail_date as d,adddate(fail_date,-row_number() over(order by fail_date asc)) as r
from failed

union all

SELECT 'succeeded'  AS period_state,success_date AS d,ADDDATE(success_date,-row_number() over(ORDER BY success_date ASC)) AS r
FROM succeeded
)a
where d between '2019-01-01' and '2019-12-31' 
GROUP BY r,period_state
order by start_date asc;

以上是关于LeetCode:Database 71.报告系统状态的连续日期的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 55.报告的记录 II

LeetCode:Database 35.学生地理信息报告

LeetCode:Database 44.项目员工 III

LeetCode:Database 103.周内每天的销售情况

LeetCode:Database 103.周内每天的销售情况

LeetCode:Database 115.仓库经理