Assume a time series where we have one value per point in time. I have to calculate a continues sum by calculating the current value + the sum of the previous value. Tricky part however is, that the sum should be capped and hence not exceed a certain value.
Example: Sum is capped at +2.
+-----+---------+------------+
| row | measure | capped sum |
+-----+---------+------------+
| 1 | 1 | 1 |
| 2 | 3 | 2 |
| 3 | 4 | 2 |
| 4 | -2 | 0 |
| 5 | 1 | 1 |
+-----+---------+------------+
e.g. the "capped sum" of row 4 is calulated using the "capped sum" from the previous row, "2", and adding the current value. As the result is < 2 we can take the value as it is.
Problem is, with HANA, I cannot use a LAG/Window function on the "capped sum" field. It will give me a "unknown column" error.
Any ideas how to resolve this in SQL/HANA SQL Script without using for loops (which would be very slow)?