I have a table that looks like the one below. I will like to find the sum of the cumulative difference between the target value daily actual value.
ID | Date | Target_value | Daily_Value
1 |01/10/20 | 200 | 5
2 |01/10/20 | 500 | 2
3 |05/10/20 | 600 | 10
1 |04/11/20 | 200 | 50
2 |05/11/20 | 500 | 80
3 |05/11/20 | 600 | 40
1 |06/12/20 | 200 | 50
4 |06/12/20 | 400 | 30
5 |07/12/20 | 300 | 20
Expected output
Date | Target_value - monthly_cummulative daily_value |
10/20 | (200 + 500 + 600) - (5 + 2 + 10) = 1283 |
11/20 | (200 + 500 + 600) - (17 + 50 + 80 + 40) = 1113 |
12/20 | (200 + 500 + 600 + 400 + 300) - (17 + 170 + 100) = 1713 |
This is similar to this Calculating Cumulative Sum in PostgreSQL but not exactly the same.