I have a sample table:
| id | start_dt | end_dt |
|---|---|---|
| 100 | 06/07/2021 | 30/09/2021 |
I would like to get the following output
| id | start_dt | end_dt |
|---|---|---|
| 100 | 06/07/2021 | 31/07/2021 |
| 100 | 01/08/2021 | 30/08/2021 |
| 100 | 01/09/2021 | 30/09/2021 |
I have tried using GENERATE_SERIES() in Amazon Redshift, but that does not give the required result.
The existing table is quite large so I could use temp tables then join back to another table at a later stage.
I have trawled through other posts, but other proposed solutions isn't quite giving the desired results / don't work at all on Amazon Redshift. Any help in solving this would be appreciated.
CodePudding user response:
The traditional method would be:
- Create a
Calendartable that contains one row per month, with start_date and end_date columns - Join your table to the
Calendartable, wheretable.start_dt <= calendar.end_dt AND table.end_dt >= calendar.start_dt - The two columns would be:
GREATEST(table.start_dt, calendar.start_dt)LEAST(table.end_dt, calendar.end_dt)
