问题
I have a table as follows:
CREATE TABLE [TBL_test] ([ID] int PRIMARY KEY,[RDate] CHAR(10),[Debtor] int,
[Creditor] int);
insert into [TBL_test] values(1,'2012/10/11',100,0);
**insert into [TBL_test] values(2,'2012/11/02',20,0);**
insert into [TBL_test] values(3,'2012/11/09',0,5);
**insert into [TBL_test] values(4,'2012/11/02',0,10);**
select *,(select sum(t2.Debtor - t2.Creditor) from TBL_test t2 where t2.rdate <=
t1.rdate
order by rdate) as RT
from TBL_test t1
order by rdate
Result
ID RDate Debtor Creditor RT
1 2012/10/11 100 0 100
2 2012/11/02 20 0 110
4 2012/11/02 0 10 110
3 2012/11/09 0 5 105
Please advice how can I do to fix the problem on
回答1:
not a proof solution, but might work for you, without the need to change datevalues
select *,(select sum(t2.Debtor - t2.Creditor) from TBL_test t2 where (t2.rdate < t1.rdate) or (t2.rdate = t1.rdate and t2.ID <= t1.ID) ) as RT
from TBL_test t1
order by rdate,ID
来源:https://stackoverflow.com/questions/13439687/running-total-order-by-date-in-sqlite