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.

Table of Contents

  1. Get Weekly AI Implementation Insights

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

Get Weekly AI Implementation Insights

Join engineering leaders who receive my analysis of common AI production failures and how to prevent them. No fluff, just actionable techniques.

Get Weekly AI Implementation Insights

Join engineering leaders who receive my analysis of common AI production failures and how to prevent them. No fluff, just actionable techniques.

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

Engineering leaders: Is your AI failing in production? Take the 10-minute assessment
>