In Redshift, when we need a sequence of dates between two given days, we can create it using the generate_series function and use it as a table in a FROM or JOIN clause.
It is useful when we need to display a table of dates and values, but we don’t have a value for each of those days. Generating the series ensures that we have no missing dates because the data source does not contain anything on that day.
Here is how we can write a subquery that generates a series of dates between the current date and the day 30 days ago:
WITH dates AS (
SELECT generate_series AS N
FROM generate_series(
(NOW()::DATE - INTERVAL 30 days),
((NOW() - interval '1 day':: DATE), '1 day')
)
)
Want to build AI systems that actually work?
Download my expert-crafted GenAI Transformation Guide for Data Teams and discover how to properly measure AI performance, set up guardrails, and continuously improve your AI solutions like the pros.