简体   繁体   English

SQL Server中的累积求和

[英]Cumulative Summation in SQL Server

I'm writing a query, part of Stored Procedure in SQL Server. 我正在写一个查询,它是SQL Server中存储过程的一部分。 I need to find cumulative summation in SQL Server. 我需要在SQL Server中找到累积总和。

A variable will hold a integer value say 100 . 变量将保存一个整数值,例如100 Let's say 比方说

Declare @Variable int = 100

Now the @NewVariable will have below formula: 现在, @NewVariable将具有以下公式:

@NewVariable = @Variable * (1 - 0.005) 

Hence @NewVariable = 99.5 因此@NewVariable = 99.5

Now, the @NewestVariable will have below formula; 现在, @NewestVariable将具有以下公式;

@NewestVariable = @NewVariable * (1 - 0.005)    

Hence the @NewestVariable will have value of 99.00 因此, @NewestVariable值为99.00

Likewise this calculation will occur 24 times and all the results will be sum at the end. 同样,此计算将进行24次,并且所有结果将在最后求和。

Hence final result will be: 100 + 99.5 +99.00 + .... ... 因此最终结果将是:100 + 99.5 +99.00 + .... ...

I tried to achieve the desired result using @Count Variable ( DECLARE @COUNT INT = 24 ) and using a While loop, but I'm not sure whether I'm correct or not? 我尝试使用@Count变量( DECLARE @COUNT INT = 24 )和While循环来达到所需的结果,但是我不确定我是否正确?

Request your help! 请求您的帮助!

Thanks! 谢谢!

You can do it using a CTE as below: 您可以使用CTE进行以下操作:

declare @variable int = 100
;with cte as
(
 select convert(numeric(8,2), @variable) as var, 1 as recCount
 union all
 select convert(numeric(8,2), @variable * (1 - recCount*0.005)) as var, recCount+1 as recCount 
 from cte
 where recCount < 24
)
select sum(var) as total from cte

Working Fiddle Demo 工作小提琴演示

Edit: Adjusted to resolve rounding error as per @peter.petrov 's comment 编辑:调整以解决根据@ peter.petrov的注释的舍入错误

If you need to get the values of each row before counting, please use this fiddle 如果需要在计数前获取每一行的值,请使用此小提琴

Try this code. 试试这个代码。

declare @Variable decimal(20,10);
set @Variable = 100;

declare @Sum decimal(20,10);
set @Sum = 0;

declare @cnt int;
set @cnt = 1;

while (@cnt <= 24) 
begin
    set @Sum = @Sum + @Variable;
    set @Variable = @Variable * (1.0 - 0.005);
    set @cnt = @cnt + 1;
end;

select @Sum;

A simple answer would be like this 一个简单的答案就是这样

DECLARE @Variable INT = 100
SELECT @Variable
DECLARE @counter INT = 1
DECLARE @SumVariable NUMERIC(20,2) = @Variable
DECLARE @NewVariable NUMERIC(20,2) = @Variable

WHILE(@counter<24)
BEGIN
    SET @NewVariable = @NewVariable * 0.995
    SET @SumVariable = @SumVariable + @NewVariable
    SET @counter = @counter+1
END
SELECT @SumVariable

You could also use a "quirky update" to avoid while-loops. 您也可以使用“新奇更新”来避免while循环。 If you're not interesed in intermediate resuls, just modify the last code line to select only the max(total) . 如果您不喜欢中间结果,则只需修改最后一个代码行以仅选择max(total)

DECLARE @total DECIMAL(10,3) = 0.000

;WITH "data" AS
(
    SELECT CAST( 100 AS DECIMAL(10,3)) AS id
    UNION ALL
    SELECT CAST( id * ( 1 - 0.005 ) AS DECIMAL(10,3)) FROM "data" WHERE id > 100 * (1 - 21 * 0.005)
)
SELECT 
    id, total = CAST( 0.000 AS DECIMAL(10,3))
INTO #temp
FROM 
    "data" 
OPTION ( MAXRECURSION 23 );

UPDATE t SET @total = total = @total + id FROM #temp t

SELECT * FROM #temp

See SQL-Fiddle for testing. 有关测试,请参见SQL-Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM