--问个收入支出凭证的实现语句
--表结构如下:
CREATE TABLE #aa(AutoID int IDENTITY (1, 1) NOT NULL ,a int,b int,c int)insert #aa
select 100,0,0 union all
select 0,100,0 union all
select 100,0,0 union all
select 100,0,0 union all
select 100,0,0 union all
select 0,100,0 union all
select 100,0,0select * from #aadrop TABLE #aa
/*
最后结果要为:
1 100 0 100
2 0 100 0
3 100 0 100
4 100 0 200
5 100 0 300
6 0 100 200
7 100 0 300
即a字段为收入字段,b字段为支出字段,c字段为余额字段
公式为:c=上一条记录的余额+收入字段-支出字段
要求不能使用游标
*/

解决方案 »

  1.   

    update #aa set c=case when (select c from #aa aaa where AutoID=(select max(AutoID) from #aa where AutoID<aaa.AutoID)) is null then 0 else (select c from #aa aaa where AutoID=(select max(AutoID) from #aa where AutoID<aaa.AutoID)) end +a-b
    自己写了一下语句,但结果不正确,实在不懂怎么写了,望高手指教
      

  2.   

    查詢Select AutoID,a,b,c=(Select SUM(a-b) From #aa  Where AutoID<=A.AutoID) From #aa A
      

  3.   

    更新
    Update A Set c=(Select SUM(a-b) From #aa  Where AutoID<=A.AutoID) From #aa A