This topic is locked

How to: Cumulative Sum

3/11/2009 4:47:20 PM
PHPRunner Tips and Tricks
Sergey Kornilov admin

Question:

I need to do a cumulative sum, with the last column updated record-by-record, in a report.

Something like this:

+-------+----+---------+

| stage | km | cum. km |

+-------+----+---------+

| 1 | 5 | 5 |

| 2 | 7 | 12 |

| 3 | 9 | 21 |

| 4 | 5 | 26 |

+-------+----+---------+


Answer:

  1. Modify SQL query to select km field twice i.e.

select ...

km,

km as cumkm

from ...


2. Set 'View as' type of cumkm to Custom and use the following code:

$_SESSION["cumkm"]+=$value;

$value=$_SESSION["cumkm"];


3. Add BeforeProcessList event to reset the value of $_SESSION["cumkm"]

$_SESSION["cumkm"]=0;