Tue 22 Sep 2009
Maybe you’d like to have a running total column in a MySQL data set so you can graph the hourly running sum of sales, for example. With MySQL, you could do something like:
mysql> SET @running_total := 0;select sum(sales) AS hour_sales, @running_total:=@running_total+ sum(sales) AS running_total, hour FROM statistics WHERE day=date(now()) GROUP BY hour;
which would produce something like:
+------------+---------------+------+ | hour_sales | running_total | hour | +------------+---------------+------+ | 48 | 48 | 0 | | 39 | 87 | 1 | | 44 | 131 | 2 | | 34 | 165 | 3 | | 61 | 226 | 4 | | 61 | 287 | 5 | | 122 | 409 | 6 | | 190 | 599 | 7 | | 209 | 808 | 8 | | 204 | 1012 | 9 | | 98 | 1110 | 10 | +------------+---------------+------+ 11 rows IN SET (0.00 sec) mysql>
thx jay pipes