Date Range Produce Series

Generate a contiguous date range using a recursive CTE for filling missing calendar gaps in time series analysis.

SqlData Fetching

Code

sqlsnippet
8 lines
WITH RECURSIVE date_range(d) AS (
  SELECT DATE '2026-01-01'
  UNION ALL
  SELECT d + INTERVAL 1 DAY
  FROM date_range
  WHERE d < DATE '2026-12-31'
)
SELECT d FROM date_range;

How the snippet works

What runs is SELECT, with date_range as the object involved.

In terms of clauses, the statements use: a CTE (WITH … AS) that names an intermediate result before the main query; a WHERE clause that keeps only the rows a statement is meant to touch; a UNION stitching together the result sets of separate queries.

Applying it

The date_range table is the working surface here.

Statements that matter

WITH RECURSIVE date_range(d) AS (
  SELECT DATE '2026-01-01'
  UNION ALL
  SELECT d + INTERVAL 1 DAY
  FROM date_range
  WHERE d < DATE '2026-12-31'
)
SELECT d FROM date_range;

Interactive Tools for This Topic

FAQ

Which tables does this file reference?
The references are `date_range`.