T-SQL 一个数字基于数据集增加的百分比

T-SQL The percentage increase of a number based on dataset

我有这样的数据集:

Year Percentage
1990 5.0
1991 7.0
1992 2.3

我想根据此数据计算数字的增长百分比。

例如:我有一个输入号码=> 100

100 的计算结果是:

100 + 5.0% = 105

105 + 7.0% = 112.35

112.35 + 2.3% = 114.93405

我可以在 T-Sql 中执行此操作吗?

CREATE function [dbo].[fn_sample] (@input decimal(29,19)) returns decimal(29,19)  as

begin

DECLARE @tmp TABLE([Year] int, [Percentage] decimal(29,19))
INSERT INTO @tmp ([Year], [Percentage]) VALUES
(1990, 5),
(1991, 7),
(1992, 2.3)

--TODO Calculate number
return @input
end

这可以完全基于集合来完成SQL。不需要循环或递归:

CREATE function [dbo].[fn_sample] (@input decimal(29,19)) returns decimal(29,19)  as
begin

    DECLARE @tmp TABLE([Year] int, [Percentage] decimal(29,19));

    INSERT INTO @tmp ([Year], [Percentage]) VALUES
    (1990, 5),
    (1991, 7),
    (1992, 2.3)
    ;

    DECLARE @output decimal(29,19) = 1.00;

    SELECT @output = EXP(SUM(LOG((100.00+Percentage)/100))) 
    FROM @tmp

    return @output;
end