我有一个 SQL Server 2012 表,其中包含客户 ID、金额和重置列。我正在尝试计算运行总计,并在重置后计算运行总计。一旦设置了重置“标志”,那么我想在运行总计之后计算运行总计。
我有以下代码,不确定是否有办法在断点后计算运行总数。
WITH a
AS
(SELECT
*
,SUM(amount) OVER (ORDER BY id) AS RunningTotal
FROM Table1)
SELECT
*
,CASE
WHEN resetYN = 1 THEN 0
ELSE Amount + LAG(RunningTotal, 1) OVER (ORDER BY id)
END AS ResetRunningTotal
FROM a