MySQL Forums
Forum List  »  Newbie

Re: cumulative query grouped by createdate,warehouse,country and location
Posted by: Peter Brawley
Date: October 02, 2018 09:41AM

One way to accumulate the value of column `x` resetting the sum when the value of column `a` changes ...

drop table if exists t;
create table t(a char(1),x int);
insert into t values(1,10),(2,20),(3,30);

set @aprev='', @xcum=0;
select a, x, @xcum:=if(a=@aprev,x+@xcum,x) as xcum
from t
join ( select @aprev:=a as a from t ) as z using(a)
order by t.a;
+------+------+------+
| a    | x    | xcum |
+------+------+------+
| 1    |   10 |   10 |
| 2    |   20 |   20 |
| 3    |   30 |   50 |
+------+------+------+

"...cumulative quantity have to be grouped by createdate, warehouse, country and location" is unclear, and doesn't match the example calculation you post, which appears to try to accumulate only on `country.

The first query you posted has an ummatched right parenthesis, so it can't run.

Options: ReplyQuote


Subject
Written By
Posted
Re: cumulative query grouped by createdate,warehouse,country and location
October 02, 2018 09:41AM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.