这是我的表名“CashTrasaction”,我想这样:
我想平衡栏自动从存款计算并提取该存款增加值以平衡和提取减去余额。 帮我怎么算!
答案 0 :(得分:0)
尝试此脚本可以获得预期的结果
;WITH cte(AcNO,Dates,CheaqueNo,CompanyId,Deposit,Withdraw,Balance)
AS
(
SELECT 2014002,2017-12-19,12500,1,1000.00,0.00,1000.00 Union all
SELECT 30520,2017-11-13,12500,7,0.00,500.00,500.00 Union all
SELECT 50021,2017-10-19,1210,45,2500.00,100.00,2900.00 Union all
SELECT 54201,2017-11-29,121100,22,6000.00,0.00,8900
)
SELECT * ,(SUM(Deposit)OVER(PArtition by AcNO Order by AcNO)
+SUM(Balance)OVER(PArtition by AcNO Order by AcNO)
-SUM(Withdraw)OVER(PArtition by AcNO Order by AcNO)
) AS CurrentBalance
FROM cte
结果
AcNO Dates CheaqueNo CompanyId Deposit Withdraw Balance CurrentBalance
-----------------------------------------------------------------------------------
30520 1993 12500 7 0.00 500.00 500.00 0.00
50021 1988 1210 45 2500.00 100.00 2900.00 5300.00
54201 1977 121100 22 6000.00 0.00 8900.00 14900.00
2014002 1986 12500 1 1000.00 0.00 1000.00 2000.00