I want to achieve the following calculation highlighted in the Desired Output column:
The highlighted row has a running total of 6.4 M, that exceeds the 5M hard limit so the SQL should return the previous row value of 3.64M.
(thus in Excel the formula would be I9=IF(I8+F9 > $H$2, I8, I8+F9)). The same logic should happen for the following rows whenever the Running total breaches the Hard limit.
| F | G | H | I | |
|---|---|---|---|---|
| SaleAmount | RunningTotal | Hard limit | Desired output | |
| 2 | 8,778.52 | 8,778.52 | 5,000,000 | 8,778.52 |
| 3 | 1,496,692.06 | 1,505,470.58 | 1,505,470.58 | |
| 4 | 1,849,601.42 | 3,355,072 | 3,355,072 | |
| 5 | 445.41 | 3,355,517.41 | 3,355,517.41 | |
| 6 | 109.98 | 3,355,627.39 | 3,355,627.39 | |
| 7 | 3,933.56 | 3,359,560.95 | 3,359,560.95 | |
| 8 | 280,915.44 | 3,640,476.39 | 3,640,476.39 | |
| 9 | 2,778,827.65 | 6,419,304.04 | 3,640,476.39 | |
| 10 | 1,349,832.28 | 7,769,136.32 | 4,990,308.67 | |
| 11 | 2,881.94 | 7,772,018.26 | 4,993,190.61 | |
| 12 | 145,824.06 | 7,917,842.32 | 4,993,190.61 | |
| 13 | 316.97 | 7,918,159.29 | 4,993,507.58 | |
| 14 | 1,181,685.55 | 9,099,844.84 | 4,993,507.58 | |
| 15 | 2,568.51 | 9,102,413.35 | 4,996,076.09 | |
| 16 | 3,000 | 9,105,413.35 | 4,999,076.09 | |
| 17 | 1,286,983.8 | 1,039,2,397.15 | 4,999,076.09 | |
| 18 | 2,735.87 | 1,039,5,133.02 | 4,999,076.09 | |
| 19 | 1,442,096.02 | 1,183,7,229.04 | 4,999,076.09 |
- F. Sale amount
- G. Running Total : Running total of Sale amount
- H. Hard Limit : Hard limit when running total exceeds this value the previous row running total should be considered
- I. ultimate output
How can I achieve this?