MYSQL cumulative sum by player -
im having table players , scores. managed create query cumulative sum. want query group cumulative sum player, cumulative sum of previous players not included start cumulative sum of new player. example of result current query:
+--------+--------+-------+ | player | total | cumul | +--------+--------+-------+ | arne | 16 | 16 | | arne | -48 | -32 | | arne | 13 | -19 | | arne | -17 | -36 | | arne | 7 | -29 | | arne | 41 | 12 | | arne | -30 | -18 | | arne | -6 | -24 | | arne | 18 | -6 | | bjorg | -5 | -11 | | bjorg | 9 | -2 | | bjorg | -38 | -40 | | bjorg | -12 | -52 | | bjorg | 11 | -41 | | bjorg | 3 | -38 | +--------+--------+-------+
how should like:
+--------+--------+-------+ | speler | total | cumul | +--------+--------+-------+ | arne | 16 | 16 | | arne | -48 | -32 | | arne | 13 | -19 | | arne | -17 | -36 | | arne | 7 | -29 | | arne | 41 | 12 | | arne | -30 | -18 | | arne | -6 | -24 | | arne | 18 | -6 | | bjorg | -5 | -5 | | bjorg | 9 | 4 | | bjorg | -38 | -34 | | bjorg | -12 | -46 | | bjorg | 11 | -35 | | bjorg | 3 | -32 | +--------+--------+-------+
the problem first table second player uses cumul of previous player start from. how should alter query in order switch between players?
here's query:
select t.player, t.total, @running_total := @running_total + t.total cumul ( select player, id, sum(punten) total `whiteboard_games` 1 group player, id) t join (select @running_total := 0) table1 order t.player, id
thanks in advance!
try query
select @score:=if(@prevplayer = player, @score+total, total) cumil, total, @prevplayer:=player player (select * tbl order player) join (select @score:=0, @prevplayer:='')b
results:
| cumil | total | player | -------------------------- | 16 | 16 | arne | | 34 | 18 | arne | | 28 | -6 | arne | | -2 | -30 | arne | | 39 | 41 | arne | | 46 | 7 | arne | | 29 | -17 | arne | | 42 | 13 | arne | | -6 | -48 | arne | | 11 | 11 | bjorg | | -1 | -12 | bjorg | | -39 | -38 | bjorg | | -30 | 9 | bjorg | | -35 | -5 | bjorg | | -32 | 3 | bjorg |
fiddle
wiki
Comments
Post a Comment