sql - Calculate a running total in MySQL -
sql - Calculate a running total in MySQL -
i have mysql query:
select dayofyear(`date`) d, count(*) `orders` `haspaid` > 0 grouping d order d
which returns this:
d | count(*) | 20 | 5 | 21 | 7 | 22 | 12 | 23 | 4 |
what i'd column on end show running total:
d | count(*) | ??? | 20 | 5 | 5 | 21 | 7 | 12 | 22 | 12 | 24 | 23 | 4 | 28 |
is possible?
perhaps simpler solution , prevents database having ton of queries. executes 1 query little math on results in single pass.
set @runtot:=0; select q1.d, q1.c, (@runtot := @runtot + q1.c) rt (select dayofyear(`date`) d, count(*) c `orders` `haspaid` > 0 grouping d order d) q1
this give additional rt (running total) column. don't miss set statement @ top initialize running total variable first or column of null values.
mysql sql
Comments
Post a Comment