0

I have a gridview with an attribute amount. I would like to do a running total ( or subtotal / rollup ) of amount for each row. E.g.:

   amount  rollup
1.   2       2
2.   3       5
3.   2       7
4.   1       8

Is there a way to do this? Can you please point me to the right direction? I have no idea how to do this, and I don't find any relevant info, sorry.

Ziki: my $dataProvider is an SqlDataProvider so I can't use this solution. Can you maybe help me a little bit out how I can adjust it to an SqlDataProvider?

I'm referring to values like $data["amount"]. It seems I don't have any key or index, can it be? I'm getting:

Undefined offset: 0 (or 1 or...)

It would be okay also if there was a SQL solution

user2511599
  • 796
  • 1
  • 13
  • 38
  • Possible duplicate of [How to get previous and next row model ID in yii2 gridview data row](https://stackoverflow.com/questions/54399991/how-to-get-previous-and-next-row-model-id-in-yii2-gridview-data-row) – Ziki Apr 15 '19 at 14:13

1 Answers1

0

I have found a simple SQL solution for that:

sum(amount) over (order by ... rows unbounded preceding)

The most important detail here is this: rows unbounded preceding

If you want the sum like this (so slide one row below):

   amount  rollup
1.   2       
2.   3       2
3.   2       5
4.   1       7

can be done with window function:

sum(amount) over (order by ... ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)

Credits to @Hart CO

user2511599
  • 796
  • 1
  • 13
  • 38