I have the following list of transactions:
| transaction_id | transaction_type | spend_year | transaction_date |
|---|---|---|---|
| 1 | New Business | 0 | 2020-11-01 |
| 2 | Supplemental | 0 | 2021-08-23 |
| 3 | Renewal | 1 | 2021-10-15 |
| 4 | Supplemental | 1 | 2022-02-01 |
| 5 | Supplemental | 1 | 2022-05-22 |
| 6 | Renewal | 2 | 2022-07-15 |
Note: spend_year is used to group transactions together based on renewal periods.
With the table above, I need to calculate two additional fields:
(1) spend_year_start_date
when spend_year = 0, this should be the min(transaction_date) (this should be the first ever transaction date)
when spend_year > 0, this should be the dateadd('month', -3, (min(transaction_date) over(partition by spend_year))) i.e. 3 months preceding the renewal transaction date for that spend year
(2) spend_year_end_date
this should be the spend_year_start_date of the next spend_year group
The desired output table should look like this:
| transaction_id | transaction_type | spend_year | transaction_date | spend_year_start_date | spend_year_end_date |
|---|---|---|---|---|---|
| 1 | New Business | 0 | 2020-11-01 | 2020-11-01 | 2021-07-15 |
| 2 | Supplemental | 0 | 2021-08-23 | 2020-11-01 | 2021-07-15 |
| 3 | Renewal | 1 | 2021-10-15 | 2021-07-15 | 2022-04-15 |
| 4 | Supplemental | 1 | 2022-02-01 | 2021-07-15 | 2022-04-15 |
| 5 | Supplemental | 1 | 2022-05-22 | 2021-07-15 | 2022-04-15 |
| 6 | Renewal | 2 | 2022-07-15 | 2022-04-15 | null |
I've been experimenting with various window functions, but still can't capture the logic correctly. Please help!