历史记录的累积更新

Accumulative Update for previous records

我有 table 显示这些信息

Month   NewClients   OnHoldClients
5-2017  10           2
6-2017  16           4
7-2017  11           1
8-2017  15           6
9-2017  18           7

我正在尝试查找每个月的累计总数 这是

(NewClients - OnHoldClients) + 上个月总计

像这样

Month   NewClients   OnHoldClients   Total
5-2017  10           2               8
6-2017  16           4               20
7-2017  11           1               30
8-2017  15           6               39
9-2017  18           7               50

我尝试构建的查询是这样的,但我认为应该是一种更简单的方法

UPDATE MyTable
SET Total = (SELECT TOP 1 Total FROM MyTable B WHERE B.Month < A.Month) + NewClients - OnHoldClients
FROM MyTable A

在我们开始之前,请注意您面临这样的计算问题这一事实本身就是一个症状,表明您可能没有最好的设计。通常为此目的,在插入记录的过程中会存储计算值。所以我想说你最好有一个总字段开始并计算它作为记录积累。

现在让我们开始解决手头的问题。我编写了一个查询,它做得很好,但由于问题的递归性质,它有点冗长。但是,它产生了准确的预期结果:

DECLARE @dmin AS date = (SELECT min(mt.[Month]) from dbo.MyTable mt);

;WITH cte(_Month, _Total) AS (
    SELECT mt.[Month] AS _Month, (mt.NewClients - mt.OnHoldClients) AS _Total
    FROM dbo.MyTable mt
    WHERE mt.[Month] = @dmin

    UNION ALL

    SELECT mt.[Month] AS _Month, ((mt.NewClients - mt.OnHoldClients) + ccc._Total) AS _Total
    FROM dbo.MyTable mt
    CROSS APPLY (SELECT cc._Total FROM (SELECT c._Total, 
                        CAST((row_number() OVER (ORDER BY c._Month DESC)) AS int) as _Rank 
                        FROM cte c WHERE c._Month < mt.[Month]) as cc
                WHERE cc._Rank = 1) AS ccc
    WHERE mt.[Month] > @dmin
)

SELECT c._Month, max(c._Total) AS Total
FROM cte c
GROUP BY c._Month

这是一个递归的 CTE 结构,它从每条记录一直到最初的月份,并将其加起来为最终的总计值。此查询仅包括 Month 和 Total 字段,但您可以轻松地将其他 2 个添加到投影列表中。

试试这个

;WITH CTE([Month],NewClients,OnHoldClients)
AS
(
SELECT '5-2017',10,2 UNION ALL
SELECT '6-2017',16,4 UNION ALL
SELECT '7-2017',11,1 UNION ALL
SELECT '8-2017',15,6 UNION ALL
SELECT '9-2017',18,7
)
SELECT [Month],
        NewClients,
        OnHoldClients,
        SUM(MonthTotal)OVER( ORDER BY [Month]) AS Total
FROM
(
SELECT [Month],
        NewClients,
        OnHoldClients,
        SUM(NewClients-OnHoldClients)OVER(PArtition by [Month] Order by [Month]) AS MonthTotal
FROM CTE
)dt

结果,演示:http://rextester.com/DKLG54359

Month   NewClients  OnHoldClients   Total
--------------------------------------------
5-2017      10          2            8
6-2017      16          4            20
7-2017      11          1            30
8-2017      15          6            39
9-2017      18          7            50