计算 运行 总余额
Calculate the Running Total Balance
我有上个月的期末余额,我想根据上个月的期末余额计算每天的 运行 余额。我试过的是下面
DECLARE @TestTable TABLE
(
id int,
somedate date,
Credit INT,
Debit INT
)
DECLARE @LastMothClosing INT=2000
insert into @TestTable values
(1, '01/Jan/20', 1000,100),
(2, '02/Jan/20', 0,0),
(3, '03/Jan/20', 500,500),
(4, '04/Jan/20', 1000,200),
(5, '05/Jan/20', 100,50)
select id,
somedate,
Credit,
Debit,
(Credit-Debit)+ (SUM(@LastMothClosing) over(order by somedate rows unbounded preceding)) as runningtotal
from @TestTable
我想把前一天的期末余额加到第二天的贷方金额上,然后从借方金额中减去得到余额等等。
我认为您求和的值有误。尝试以下操作:
select id
, SomeDate
, Credit
, Debit
, @LastMothClosing + sum(Credit-Debit) over (order by somedate rows unbounded preceding) as RunningTotal
from @TestTable;
给出:
id SomeDate Credit Debit RunningTotal
--------------------------------------------
1 2020-01-01 1000 100 2900
2 2020-01-02 0 0 2900
3 2020-01-03 500 500 2900
4 2020-01-04 1000 200 3700
5 2020-01-05 100 50 3750
我有上个月的期末余额,我想根据上个月的期末余额计算每天的 运行 余额。我试过的是下面
DECLARE @TestTable TABLE
(
id int,
somedate date,
Credit INT,
Debit INT
)
DECLARE @LastMothClosing INT=2000
insert into @TestTable values
(1, '01/Jan/20', 1000,100),
(2, '02/Jan/20', 0,0),
(3, '03/Jan/20', 500,500),
(4, '04/Jan/20', 1000,200),
(5, '05/Jan/20', 100,50)
select id,
somedate,
Credit,
Debit,
(Credit-Debit)+ (SUM(@LastMothClosing) over(order by somedate rows unbounded preceding)) as runningtotal
from @TestTable
我想把前一天的期末余额加到第二天的贷方金额上,然后从借方金额中减去得到余额等等。
我认为您求和的值有误。尝试以下操作:
select id
, SomeDate
, Credit
, Debit
, @LastMothClosing + sum(Credit-Debit) over (order by somedate rows unbounded preceding) as RunningTotal
from @TestTable;
给出:
id SomeDate Credit Debit RunningTotal
--------------------------------------------
1 2020-01-01 1000 100 2900
2 2020-01-02 0 0 2900
3 2020-01-03 500 500 2900
4 2020-01-04 1000 200 3700
5 2020-01-05 100 50 3750