declare @t table (id int,money int)insert into @t
select 1,100
union all select 2,20
union all select 3,-10
union all select 4,8select id,money, (select sum(money) from @t where id<=a.id ) as balance from @t a /*
id money balance
1 100 100
2 20 120
3 -10 110
4 8 118
*/
select 1,100
union all select 2,20
union all select 3,-10
union all select 4,8select id,money, (select sum(money) from @t where id<=a.id ) as balance from @t a /*
id money balance
1 100 100
2 20 120
3 -10 110
4 8 118
*/
(select sum([money]) from tablename where id<=a.id) as balance
from tablename