Date Range Produce Series
Generate a contiguous date range using a recursive CTE for filling missing calendar gaps in time series analysis.
Code
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
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Date Add/Subtract CalculatorAdd or subtract days, weeks, months, or years from a date, clamping month ends such as January 31 plus one month.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.
FAQ
- Which tables does this file reference?
- The references are `date_range`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.