为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1225. 报告系统状态的连续日期 | Report Contiguous Dates

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

SQL架构:

 1 Create table If Not Exists Failed (fail_date date)
 2 Create table If Not Exists Succeeded (success_date date)
 3 Truncate table Failed
 4 insert into Failed (fail_date) values ('2018-12-28')
 5 insert into Failed (fail_date) values ('2018-12-29')
 6 insert into Failed (fail_date) values ('2019-01-04')
 7 insert into Failed (fail_date) values ('2019-01-05')
 8 Truncate table Succeeded
 9 insert into Succeeded (success_date) values ('2018-12-30')
10 insert into Succeeded (success_date) values ('2018-12-31')
11 insert into Succeeded (success_date) values ('2019-01-01')
12 insert into Succeeded (success_date) values ('2019-01-02')
13 insert into Succeeded (success_date) values ('2019-01-03')
14 insert into Succeeded (success_date) values ('2019-01-06')

Table: Failed

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| fail_date | date |
+--------------+---------+
Primary key for this table is fail_date.
Failed table contains the days of failed tasks.
Table: Succeeded

+--------------+---------+
| 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:

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 |
+--------------+--------------+--------------+
| present | 2019-01-01 | 2019-01-03 |
| missing | 2019-01-04 | 2019-01-05 |
| present | 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 "present".
From 2019-01-04 to 2019-01-05 all tasks failed and system state was "missing".
From 2019-01-06 to 2019-01-06 all tasks succeeded and system state was "present".

posted @ 2019-10-21 12:36  为敢技术  阅读(369)  评论(0编辑  收藏  举报