我试图从这些数据中获得平衡
Date Logged Closed
-------------- ----------- -----------
1-Jan-2016 0 0
2-Jan-2016 8 7
3-Jan-2016 8 8
4-Jan-2016 25 11
5-Jan-2016 20 16
6-Jan-2016 14 13
7-Jan-2016 10 12
8-Jan-2016 9 7
9-Jan-2016 12 12
10-Jan-2016 3 4
预期输出
Date Logged Closed Balance
-------------- ----------- ----------- ----------
1-Jan-2016 0 0 0
2-Jan-2016 8 7 1
3-Jan-2016 8 8 1
4-Jan-2016 25 11 15
5-Jan-2016 20 16 19
6-Jan-2016 14 13 20
7-Jan-2016 10 12 18
8-Jan-2016 9 7 20
9-Jan-2016 12 12 20
10-Jan-2016 3 4 27
公式为BALANCE = PREVIOUSBALANCE + LOGGED - CLOSED。
示例公式:
Jan 5 Balance (15) = 1(prevBalance) + 25(currentLogged) - 11(currentClosed)
我已经尝试过这个公式,但没有达到预期结果的任何地方。
WITH CTE AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY Date),
Date, Logged, Closed
FROM Table
)
SELECT
(prev.Logged - prev.Closed)+ (a.Logged-a.Closed) as [Balance]
FROM CTE
LEFT JOIN CTE prev ON prev.rownum = CTE.rownum - 1
使用其他参考资料
SQL Server - Calculate current row value using previous row value
答案 0 :(得分:2)
您可以使用窗口函数sum
执行此操作。
试试这个:
select
t.*,
sum(logged - closed) over (order by date) balance
from your_table t;
答案 1 :(得分:0)
您的答案问题是计算时无法使用前一行的平衡(SQL-Server的一次性原则)。您的问题隐藏了计算所有先前行的总和的要求,按日期排序。
尝试此操作(仅适用于SQL Server 2012及更高版本)
SELECT
[Date], Logged, Closed,
SUM(Logged) OVER (ORDER BY [Date] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
- SUM (Closed) OVER (ORDER BY [Date] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Balance
FROM
<<Table>>
对于以前的版本:
SELECT
t.[Date], t.Logged, t.Closed, sub.L - sub.C AS Balanced
FROM
<<Table>> t
CROSS APPLY ( SELECT SUM(Logged) AS L, SUM(Closed) AS c FROM <<Table>> WHERE [Date] <= t.[Date]) AS sub