6
使用窗口功能的日期范围滚动总和
我需要计算日期范围内的滚动总和。为了说明这一点,使用AdventureWorks示例数据库,以下假设语法将完全满足我的需要: SELECT TH.ProductID, TH.TransactionDate, TH.ActualCost, RollingSum45 = SUM(TH.ActualCost) OVER ( PARTITION BY TH.ProductID ORDER BY TH.TransactionDate RANGE BETWEEN INTERVAL 45 DAY PRECEDING AND CURRENT ROW) FROM Production.TransactionHistory AS TH ORDER BY TH.ProductID, TH.TransactionDate, TH.ReferenceOrderID; 可悲的是,RANGE窗口框架范围当前在SQL Server中不允许间隔。 我知道我可以使用子查询和常规(非窗口)聚合来编写解决方案: SELECT TH.ProductID, TH.TransactionDate, TH.ActualCost, RollingSum45 = ( SELECT SUM(TH2.ActualCost) FROM Production.TransactionHistory AS TH2 …