Approach
Relational aggregation
For Report Contiguous Dates, the query transforms and combines relational rows, then filters or aggregates them into the requested result.
- Identify the source rows and join keys.
- Apply filters before aggregation when possible.
- Group, rank, or project the final columns required by the result.
Code notes
- 32 lines of SQL from the credited upstream file 1225.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Review join cardinality, grouping keys, and available indexes when estimating query cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH2 RankedDatesPerState AS (3 SELECT4 'failed' AS state,5 fail_date AS `date`,6 RANK() OVER(ORDER BY fail_date) AS rank_per_state7 FROM Failed8 WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31'9 UNION ALL10 SELECT11 'succeeded' AS state,12 success_date AS `date`,13 RANK() OVER(ORDER BY success_date) AS rank_per_state14 FROM Succeeded15 WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31'16 ),17 RankedDates AS (18 SELECT19 state,20 `date`,21 rank_per_state,22 RANK() OVER(ORDER BY `date`) AS `rank`23 FROM RankedDatesPerState24 )25SELECT26 state AS period_state,27 MIN(`date`) AS start_date,28 MAX(`date`) AS end_date29FROM RankedDates30GROUP BY state, (`rank` - rank_per_state)31ORDER BY start_date32