我正在尝试在 SQL Server 2012 中编写一个视图,在其中计算一组事务的 30 天移动总和。
当前脚本:
SELECT CustNo, TransactionDate, TransactionAmount, SUM(TransactionAmount)
OVER (PARTITION BY CustNo ORDER BY TransactionDate) AS MovingAmount
FROM dbo.TransactionData
数据集:
CustNo TransactionDate TransactionAmount
1111 5/7/2015 3,000
1111 5/14/2015 3,000
2222 5/17/2015 100
1111 5/21/2015 3,000
1111 5/28/2015 3,000
3333 5/31/2015 11,000
1111 6/10/2015 3,000
预期结果:
CustNo TransactionDate TransactionAmount MovingAmount
1111 5/7/2015 3,000 12,000
1111 5/14/2015 3,000 12,000
2222 5/17/2015 100 100
1111 5/21/2015 3,000 9,000
1111 5/28/2015 3,000 6,000
3333 5/31/2015 11,000 11,000
1111 6/10/2015 3,000 3,000
其他尝试:
SELECT CustNo, TransactionDate, TransactionAmount, SUM(TransactionAmount)
OVER (PARTITION BY CustomerNumber ORDER BY TransactionDate,
BETWEEN TransactionDate AND DATEADD(day, 30, TransactionDate))
AS MovingAmount
FROM dbo.TransactionData