Calculate running total / running balance

后端 未结 6 1853
粉色の甜心
粉色の甜心 2020-11-22 10:01

I have a table:

create table Transactions(Tid int,amt int)

With 5 rows:

insert into Transactions values(1, 100)
insert into         


        
6条回答
  •  一生所求
    2020-11-22 10:40

    If you use version 2012, here is a solution

    select *, sum(amt) over (order by Tid) as running_total from Transactions 
    

    For earlier versions

    select *,(select sum(amt) from Transactions where Tid<=t.Tid) as running_total from Transactions as t
    

提交回复
热议问题