MySQL calculation of cumulative sum with a reset condition -


is there way capture cummulative sum reset condition in mysql? use case simple cash register table

id    transactiontype      value  currentbalance 1      purchase             10        10 2      sale                -10        0 3      reset                20        20 4      purchase             10        30 5      sale                -10        20 6      sale                 10        30 7      sale                -20       10 

the answer here starting point don't see how extend it: create cumulative sum column in mysql

select t.id,          t.count,          @running_total := @running_total + t.value currentbalance     table t     join (select @running_total := 0) r order t.id 

note: idea reset cumulative sum each time reset hit. ideally trying have on update/insert trigger update entire currentbalance column taking account resets. small table don't mind updating whole table in exchange simple.

all requires simple conditional logic:

select t.id, t.count,        @running_total := if(transactiontype = 'reset', t.value,                             @running_total + t.value                            ) currentbalance table t join      (select @running_total := 0) params order t.id; 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -