以前记录的累积更新

时间:2018-05-16 01:07:57

标签: sql-server sql-update accumulate

我有表格显示这些信息

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

2 个答案:

答案 0 :(得分:1)

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

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

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结构,它一直到初始月份的每条记录,并累加到最终的总值。此查询仅包括“月”和“总计”字段,但您可以轻松地将其他2添加到投影列表中。

答案 1 :(得分:1)

试试这个

;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