I have a table in amazon redshift (datagrip) like so:
Contract_ID | Starting_Month | Contract_Duration_In_Months | Collection_Due_Date | Target | Amount_Collected |
---|---|---|---|---|---|
10001 | 01/01/2022 | 12 | 01/01/2022 | 10000 | 40000 |
10001 | 01/01/2022 | 12 | 01/02/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/03/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/04/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/05/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/06/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/07/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/08/2022 | 10000 | 30000 |
10001 | 01/01/2022 | 12 | 01/09/2022 | 10000 | 2500 |
10001 | 01/01/2022 | 12 | 01/10/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/11/2022 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/12/2022 | 10000 | 0 |
10002 | 01/01/2022 | 8 | 01/03/2022 | 5000 | 12000 |
10002 | 01/01/2022 | 8 | 01/04/2022 | 5000 | 1000 |
10002 | 01/01/2022 | 8 | 01/05/2022 | 5000 | 0 |
10002 | 01/01/2022 | 8 | 01/06/2022 | 5000 | 0 |
10002 | 01/01/2022 | 8 | 01/07/2022 | 5000 | 10000 |
10002 | 01/01/2022 | 8 | 01/08/2022 | 5000 | 0 |
10002 | 01/01/2022 | 8 | 01/09/2022 | 5000 | 0 |
10002 | 01/01/2022 | 8 | 01/10/2022 | 5000 | 0 |
Whereby each month I have a target amount and a payment (amount_collected), and each month I want to see how much of my target I've achieved (the achieved amount can never be higher than the target), if the payment exceeds the target then the amount achieved will trickle down to the next month. Like so:
Contract_ID | Starting_Month | Contract_Duration_In_Months | Collection_Due_Date | Target | Amount_Collected | Achieved | Overpayment |
---|---|---|---|---|---|---|---|
10001 | 01/01/2022 | 12 | 01/01/2022 | 10000 | 40000 | 10000 | 30000 |
10001 | 01/01/2022 | 12 | 01/02/2022 | 10000 | 0 | 10000 | 20000 |
10001 | 01/01/2022 | 12 | 01/03/2022 | 10000 | 0 | 10000 | 10000 |
10001 | 01/01/2022 | 12 | 01/04/2022 | 10000 | 0 | 10000 | 0 |
10001 | 01/01/2022 | 12 | 01/05/2022 | 10000 | 0 | 0 | 0 |
10001 | 01/01/2022 | 12 | 01/06/2022 | 10000 | 0 | 0 | 0 |
10001 | 01/01/2022 | 12 | 01/07/2022 | 10000 | 0 | 0 | 0 |
10001 | 01/01/2022 | 12 | 01/08/2022 | 10000 | 30000 | 10000 | 20000 |
10001 | 01/01/2022 | 12 | 01/09/2022 | 10000 | 2500 | 10000 | 12500 |
10001 | 01/01/2022 | 12 | 01/10/2022 | 10000 | 0 | 10000 | 2500 |
10001 | 01/01/2022 | 12 | 01/11/2022 | 10000 | 0 | 2500 | 0 |
10001 | 01/01/2022 | 12 | 01/12/2022 | 10000 | 0 | 0 | 0 |
10002 | 01/01/2022 | 8 | 01/03/2022 | 5000 | 12000 | 5000 | 7000 |
10002 | 01/01/2022 | 8 | 01/04/2022 | 5000 | 1000 | 5000 | 3000 |
10002 | 01/01/2022 | 8 | 01/05/2022 | 5000 | 0 | 3000 | 0 |
10002 | 01/01/2022 | 8 | 01/06/2022 | 5000 | 0 | 0 | 0 |
10002 | 01/01/2022 | 8 | 01/07/2022 | 5000 | 10000 | 5000 | 5000 |
10002 | 01/01/2022 | 8 | 01/08/2022 | 5000 | 0 | 5000 | 0 |
10002 | 01/01/2022 | 8 | 01/09/2022 | 5000 | 0 | 0 | 0 |
10002 | 01/01/2022 | 8 | 01/10/2022 | 5000 | 0 | 0 | 0 |
I can't use SUM() OVER(ORDER BY MONTH ROWS UNBOUNDED PRECEDING) because even though the achievements in May, June and July were missed, the achieved column only looks forward, not backwards. I think the best way to achieve this would be to have an Overpayment column which is like a rolling sum of the previous overpayment values - the previously achieved value BUT if the overpayment was previously 0 then we have a month where there is an overpayment the previously rolling sum needs to be discarded and the overpayment value reset to the payment-target.
Simply put, I need overpayments to trickle to down the achieved columns over the months taking into consideration the payments made until overpayments are finished, ignoring any months where the target wasn't achieved.