2

可能重复:
在 SqlServer 中计算运行总计

我需要使用前一行中的值来生成累积值,如下所示。对于 2000 年的每个代码,起始基数始终为 100。我需要使用 tsql 代码来实现这一点。

id                Code              Yr             Rate           Base        

1                   4               2000           5              100                                  
2                   4               2001           7              107 (100+7)             
3                   4               2002           4              111 (107+4)                              
4                   4               2003           8              119 (111+8)
5                   4               2004           10             129 (119+10)
6                   5               2000           2              100
7                   5               2001           3              103 (100+3)
8                   5               2002           8              111 (103+8)
9                   5               2003           5              116 (111+5)
10                  5               2004           4              120 (116+4) 
4

1 回答 1

0

好的。我们有这样的表 CREATE Table MyTbl(id INT PRIMARY KEY IDENTITY(1,1), Code INT, Yr INT, Rate INT) 我们想通过代码计算累积值。所以我们可以像这样使用查询:

1)递归(需要更多资源,但输出结果如示例所示)

with cte as
(SELECT *, ROW_NUMBER()OVER(PARTITION BY Code ORDER BY Yr ASC) rn
FROM MyTbl),

recursion as
(SELECT id,Code,Yr,Rate,rn, CAST(NULL as int) as Tmp_base, CAST('100' as varchar(25)) AS Base FROM cte
WHERE rn=1
UNION ALL
SELECT cte.id,cte.Code,cte.Yr,cte.Rate,cte.rn, 
CAST(recursion.Base as int),
CAST(recursion.Base+cte.Rate as varchar(25))
FROM recursion JOIN cte ON recursion.Code=cte.Code AND recursion.rn+1=cte.rn
)

SELECT id,Code,Yr,Rate, 
CAST(Base as varchar(10))+ISNULL(' ('+ CAST(Tmp_base as varchar(10))+'+'+CAST(Rate as varchar(10))+')','') AS Base 
FROM recursion
ORDER BY 1


OPTION(MAXRECURSION 0)

2)或者我们可以使用更快的查询而不使用递归。但结果是不可能生成像'107(100+7)'这样的字符串(只有像'107'这样的字符串)

SELECT *,

    100 +
    (SELECT ISNULL(SUM(rate),0) /*we need to calculate only the sum in subquery*/
    FROM MyTbl AS a
    WHERE
        a.Code=b.Code /*the year in subquery equals the year in main query*/
        AND a.Yr<b.Yr /*main feature in our subquery*/
        ) AS base

FROM MyTbl AS b
于 2012-09-22T17:38:31.817 回答