I have 3 columns as user_id
, start_date
, end_date
. as an example;
user_id start_date end_date
1 2022-07-30 2025-07-30
2 2022-05-25 2027-05-25
I want to create rows based on start_date and end_date with yearly laps on the same date. so it should be like as below;
user_id start_date end_date
1 2022-07-30 2023-07-30
1 2023-07-30 2024-07-30
1 2024-07-30 2025-07-30
2 2022-05-25 2023-05-25
2 2023-05-25 2024-05-25
2 2024-05-25 2025-05-25
2 2025-05-25 2026-05-25
2 2026-05-25 2027-05-25
how can I do that?
Some remarks:
I’m using AWS Redshift environment. I couldn’t use recursive CTE since it is in the middle of a very long query. If I’m not wrong recursive CTEs has to start with ‘WITH’ clause.
edit: thanks for all the answers and they are answering the specific request above but I miss a very important part(it was only user 1). for every user end date is different. For above example it is 2 years but for some users it is 5 years in between. so it should be dynamic, I added another example.
3
Answers
Something like:
A CTE is (or can be) basically just a sub-query.
OK, so with the changed requirements
This form of
generate_series
produces a timestamptz so you need to cast it back to a date.UPDATED
Cover variable length of years for initial start and end values. This assumes that each start/end period is always going to be 1 year in length.
first filter out rows that ‘MM-DD’ (month,day) are the same.
then for the each matched remaining rows, duplicate (end_date’s year – start_date’s year + 1 row.
It’s a common table expression (CTE), but it’s not recursive.CTE tend to be more readable.