How to generate a sequence of dates in Redshift

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (82/100)

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')

Subscribe to the newsletter and join the free email course.

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Would you like to have a call and talk? Please schedule a meeting using this link.

Bartosz Mikulski
Bartosz Mikulski * MLOps Engineer / data engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

Subscribe to the newsletter and get access to my free email course on building trustworthy data pipelines.