Mysql 得到累积的总和

Mysql 得到累积的总和,mysql,Mysql,假设mysql数据库中有一个表,如下所示 date inQty outQty 2011-10-24 700.00 0.0 2011-10-01 500.00 0.0 2011-10-02 500.00 0.0 2011-10-03 550.00 0.0 2011-10-04 100.00 0.0 2011-10-05 200.00 0.0 2011-10-05 0.00 100.0 2011-10-02 0.00 500.0 2011-

假设mysql数据库中有一个表,如下所示

date        inQty   outQty
2011-10-24  700.00  0.0
2011-10-01  500.00  0.0
2011-10-02  500.00  0.0
2011-10-03  550.00  0.0
2011-10-04  100.00  0.0 
2011-10-05  200.00  0.0
2011-10-05  0.00    100.0
2011-10-02  0.00    500.0
2011-10-03  0.00    150.0
2011-10-24  200.00  0.0
从上表中,我需要查询以下结果

date        inQty   outQty   Balance
2011-10-24  700.00  0.0      700.00
2011-10-01  500.00  0.0  500.00
2011-10-02  500.00  0.0  500.00
2011-10-03  550.00  0.0      550.00
2011-10-04  100.00  0.0  100.00
2011-10-05  200.00  0.0      200.00
2011-10-05  0.00    100.0    100.00
2011-10-02  0.00    500.0    0.0
2011-10-03  0.00    150.0    400.00
2011-10-24  200.00  0.0      500.00

如何从SQL查询中获取此信息

我想你是在寻找分区上的累积和。不过这只是一个猜测,因为你的帖子并不清楚,因为数据没有清晰的模式。我想我明白你的意思

也许你可以回顾和编辑你的帖子,让问题更清楚

无论如何,试试看。我不知道你的表叫什么,所以我将发布我自己的示例

create table dateCumulative
(vDate date not null,
inQty decimal (12,2) not null default 0.0,
outQty decimal (12,2) not null default 0.0
);

insert into dateCumulative values ('2011-10-24',700.00,0.0);
insert into dateCumulative values ('2011-10-01',500.00,0.0);
insert into dateCumulative values ('2011-10-02',500.00,0.0);
insert into dateCumulative values ('2011-10-03',550.00,0.0);
insert into dateCumulative values ('2011-10-04',100.00,0.0);
insert into dateCumulative values ('2011-10-05',200.00,0.0);
insert into dateCumulative values ('2011-10-05',0.00,100.0);
insert into dateCumulative values ('2011-10-02',0.00,500.0);
insert into dateCumulative values ('2011-10-03',  0.00    ,150.0);
insert into dateCumulative values ('2011-10-24',  200.00  ,0.0);

select t.vDate,t.inQty,t.outQty,
round(t.inQtySum-t.outQtySum,2) as balance
from
( 
select 
(case when vDate = @curDate then (@inCsum := @inCsum + inQty) else @inCsum := inQty   end) as inQtySum,
(case when vDate = @curDate then (@outCsum := @outCsum + outQty) else @outCsum := outQty end) as outQtySum,
(case when vDate != @curDate then @curDate := vDate end) as dateChanged,
dc.*
from dateCumulative dc
inner join (SELECT @curDate := '1970-01-01',@inCsum:=0,@outCsum:=0) as t
order by vDate asc
) t;

您的示例没有任何意义第一个数据集中的0.0900.00是什么?为什么最后一行的余额是500?不是应该是900吗?检查并纠正您的示例。