I am trying to get the column cumulative_point
like below.
date | point | reset_date | cumulative_point |
---|---|---|---|
2019-01-01 00:00:00 | 1 | true | 1 |
2019-01-02 00:00:00 | 3 | false | 4 |
2019-01-03 00:00:00 | 1 | false | 5 |
2019-01-04 00:00:00 | 2 | false | 7 |
2019-01-05 00:00:00 | 1 | true | 1 |
2019-01-06 00:00:00 | 4 | false | 5 |
2019-01-07 00:00:00 | 2 | false | 7 |
cumulative_point
is a cumulative value of point
column, then reset it when reset_date
column is true
and doesn’t take over the previous cumulative value.
I have been trying to do this for a while but I couldn’t figure out how to reset cumulative_point
when the reset_date
column is true
.
I did like
SUM(CASE WHEN reset_date then 0 ELSE point END) as cumulative_point
but then the SUM
function added values to the previous value like below.
date | point | reset_date | cumulative_point |
---|---|---|---|
2019-01-01 00:00:00 | 1 | true | 1 |
2019-01-02 00:00:00 | 3 | false | 4 |
2019-01-03 00:00:00 | 1 | false | 5 |
2019-01-04 00:00:00 | 2 | false | 7 |
2019-01-05 00:00:00 | 1 | true | 1 |
2019-01-06 00:00:00 | 4 | false | 12 |
2019-01-07 00:00:00 | 2 | false | 14 |
How can I reset the cumulative values by sum() when reset_date
is true and ignore the previous cumulative values?
Thank you!
2
Answers
dbfiddle
create a view:
query:
basic idea is
reset_date
is boolean. I added more test case. Hope covered all the corner case. using window function can solve the problem.You may use the running sum of ‘reset_date’ (1 where true, 0 where false) to define a new group whenever a true value is shown, then use the cumulative sum of ‘point’ partitioned by that group.
See demo.