Skip to content

1225. Report Contiguous Dates 👍

 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
WITH
  RankedDatesPerState AS (
    SELECT
      'failed' AS state,
      fail_date AS `date`,
      RANK() OVER(ORDER BY fail_date) AS rank_per_state
    FROM Failed
    WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31'
    UNION ALL
    SELECT
      'succeeded' AS state,
      success_date AS `date`,
      RANK() OVER(ORDER BY success_date) AS rank_per_state
    FROM Succeeded
    WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31'
  ),
  RankedDates AS (
    SELECT
      state,
      `date`,
      rank_per_state,
      RANK() OVER(ORDER BY `date`) AS `rank`
    FROM RankedDatesPerState
  )
SELECT
  state AS period_state,
  MIN(`date`) AS start_date,
  MAX(`date`) AS end_date
FROM RankedDates
GROUP BY state, (`rank` - rank_per_state)
ORDER BY start_date