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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -