This is my sample dataset. Seq_no and value columns are given, Cum.sum and Bucket columns we need to find using postgresql. To Find cumulative sum , i can write sum(value) over (order by seq_no rows between unbounded preceding and current row)
. But ask is reset cum.sum if value reach threshold value. For example Threshold value is 20 and sum of the row 1,2 and 3 values are 23 which is greater than 20 . so we reset the window function. The next threshold value calculate from seq_no 4 onwards .
Question posted in PostgreSQL
The official documentation can be found here.
The official documentation can be found here.
2
Answers
try using this sql query for calculating the cumulative sum based on threshold value and resetting the sun after reaching the threshold value each time.
Couldn’t figure out a way to do it with pure SQL, but here’s a plpgsql routine that works: