1

我有一张如下表:

    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

结果

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

请建议我该如何解决这个问题

4

1 回答 1

0

不是证明解决方案,但可能对您有用,无需更改日期值

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
于 2012-11-29T12:36:25.213 回答