返回介绍

solution / 1200-1299 / 1225.Report Contiguous Dates / README_EN

发布于 2024-06-17 01:03:21 字数 4291 浏览 0 评论 0 收藏 0

1225. Report Contiguous Dates

中文文档

Description

Table: Failed

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| fail_date  | date  |
+--------------+---------+
fail_date is the primary key (column with unique values) for this table.
This table contains the days of failed tasks.

 

Table: Succeeded

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| success_date | date  |
+--------------+---------+
success_date is the primary key (column with unique values) for this table.
This 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 a solution to report the 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.

Return the result table ordered by start_date.

The result format is in the following example.

 

Example 1:

Input: 
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    |
+-------------------+
Output: 
+--------------+--------------+--------------+
| 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   |
+--------------+--------------+--------------+
Explanation: 
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 the system state was "failed".
From 2019-01-06 to 2019-01-06 all tasks succeeded and the system state was "succeeded".

Solutions

Solution 1: Union + Window Function + Group By

We can merge the two tables into one table with a field st representing the status, where failed indicates failure and succeeded indicates success. Then, we can use a window function to group the records with the same status into one group, and calculate the difference between each date and its rank within the group as pt, which serves as the identifier for the same continuous status. Finally, we can group by st and pt, and calculate the minimum and maximum dates for each group, and sort by the minimum date.

# Write your MySQL query statement below
WITH
  T AS (
    SELECT fail_date AS dt, 'failed' AS st
    FROM Failed
    WHERE YEAR(fail_date) = 2019
    UNION ALL
    SELECT success_date AS dt, 'succeeded' AS st
    FROM Succeeded
    WHERE YEAR(success_date) = 2019
  )
SELECT
  st AS period_state,
  MIN(dt) AS start_date,
  MAX(dt) AS end_date
FROM
  (
    SELECT
      *,
      SUBDATE(
        dt,
        RANK() OVER (
          PARTITION BY st
          ORDER BY dt
        )
      ) AS pt
    FROM T
  ) AS t
GROUP BY 1, pt
ORDER BY 2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文