How to generate a sequence of dates in Redshift

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

How to assign rows to ranked groups in AWS Athena

How to use the NTILE function in Athena

Newer post

How to index data in Redshift

How to create an equivalent of an index in Redshift