select id,in1,out1,qty=isnull((select sum(in1) from tb where id<a.id),0)+in1-out1 from tb a

解决方案 »

  1.   

    select 
        a.ID,a.In,a.Out,Qty=sum(b.In-b.Out) 
    from 
        Balance a,Balance b 
    where 
        a.ID>=b.ID 
    group by 
        a.ID,a.In,a.Out
      

  2.   

    select 
        a.ID,a.In,a.Out,Qty=sum(b.In-b.Out) 
    from 
        Balance a,Balance b 
    where 
        a.ID>=b.ID 
    group by 
        a.ID,a.In,a.Out
      

  3.   

    select 
        a.ID,a.In,a.Out,Qty=(select sum(b.In-b.Out) from Balance where ID<=a.ID) 
    from 
        Balance a
      

  4.   

    select 
        a.ID,a.In,a.Out,Qty=(select sum(b.In-b.Out) from Balance where ID<=a.ID) 
    from 
        Balance a
    对 我还考虑少了些问题
      

  5.   

    select B.*,(select sum(In-Out) from Balance where id<=B.id) as Qty
    from Balance B
      

  6.   

    select id,in1,out1,
    qty=isnull((select top 1 Qty from tb where id<a.id order by ID desc),0)+in1-out1 from tb a