Skip to content
Advertisement

Repeat Rows Between Date Values in Redshift

I have a table:

and I would like to repeat rows based on the time between start_date and end_date, in this case by years extracted from those two date columns. My desired result would resemble:

How can I achieve this in Redshift?

Advertisement

Answer

We can try joining with a calendar table containing all years which would appear in your table:

enter image description here

Demo

Note: Use DATE_PART(year, t2.start_date) for Redshift, where the datetime component does not take single quotes.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement