当前位置:网站首页>Calculate running total / running balance

Calculate running total / running balance

2022-06-13 07:09:00 French red wine sweet

problem :

I have a table: I have a table :

create table Transactions(Tid int,amt int)

With 5 rows:5 That's ok :

insert into Transactions values(1, 100)insert into Transactions values(2, -50)insert into Transactions values(3, 100)insert into Transactions values(4, -100)insert into Transactions values(5, 200)

Desired output: Required output :

TID  amt  balance--- ----- -------1    100   1002    -50    503    100   1504   -100    505    200   250

Basically for first record balance will be same as amt , 2nd onwards balance would be addition of previous balance + current amt . Basically , The balance of the first record will be the same as amt identical , The second subsequent balance will be the previous balance + At present amt Add .I am looking for an optimal approach. I am looking for the best way .I could think about using function or correlated subquery but not sure exactly how to do it. I might consider using functions or related subqueries , But I'm not sure exactly how to do it .


Solution :

Reference resources : https://stackoom.com/en/question/lSTV
原网站

版权声明
本文为[French red wine sweet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550302451.html