One task runs per day and either fails or succeeds. Failed days are recorded in Failed and successful days in Succeeded. For each maximal interval of consecutive days with the same outcome between 2019-01-01 and 2019-12-31, return period_state ('failed' or 'succeeded'), start_date and end_date, ordered by start_date.
Input Table(s)
Failed
| Column | Type | Meaning |
|---|
| fail_date | DATE | A day on which the task failed |
Primary key: fail_date.
Succeeded
| Column | Type | Meaning |
|---|
| success_date | DATE | A day on which the task succeeded |
Primary key: success_date.
Output Contract
-
Columns, in this order:
period_state
(text,
'failed'
or
'succeeded'
),
start_date
(DATE),
end_date
(DATE).
-
One row per maximal period.
start_date
and
end_date
are the first and last day of the period, both inclusive. A single isolated day gives a row whose
start_date
equals its
end_date
.
-
Ordered by
start_date
ascending. Periods never overlap, so
start_date
is unique and the order is fully determined.
Example
Input Failed:
| fail_date |
|---|
| 2019-01-01 |
| 2019-01-02 |
| 2019-01-03 |
| 2019-01-06 |
| 2019-01-07 |
Input Succeeded:
| success_date |
|---|
| 2019-01-04 |
| 2019-01-05 |
| 2019-01-08 |
| 2019-01-09 |
| 2019-01-10 |
Expected result:
| period_state | start_date | end_date |
|---|
| failed | 2019-01-01 | 2019-01-03 |
| succeeded | 2019-01-04 | 2019-01-05 |
| failed | 2019-01-06 | 2019-01-07 |
| succeeded | 2019-01-08 | 2019-01-10 |
Days 2019-01-01 to 2019-01-03 are consecutive failed days, 2019-01-04 to 2019-01-05 are consecutive succeeded days, 2019-01-06 to 2019-01-07 failed again, and 2019-01-08 to 2019-01-10 succeeded.
A second, smaller case shows what a missing day does. With Failed = 2019-03-01, 2019-03-02, 2019-03-04 and Succeeded = 2019-03-05, the result is:
| period_state | start_date | end_date |
|---|
| failed | 2019-03-01 | 2019-03-02 |
| failed | 2019-03-04 | 2019-03-04 |
| succeeded | 2019-03-05 | 2019-03-05 |
2019-03-03 has no row in either table, so the failed days on either side of it are not consecutive and form two periods.
Constraints and Clarifications
-
No day has more than one event: a date never appears in both tables, and each table has at most one row per date.
-
The report covers 2019-01-01 through 2019-12-31. Dates outside that range, if present, are ignored, and a period that would extend past either end is cut at the boundary.
-
Consecutive means calendar-consecutive. A day with no row in either table ends the current period.
-
Neither table contains NULLs.