递归乘法sql server

时间:2015-07-01 13:43:02

标签: sql-server recursion sql-server-2012

有没有办法将一个数字乘以一个比率,这个乘法的结果乘以另一个比率,再将这最后一个数字的结果乘以另一个比率?

乘法将是:

100 + 100 * 0.1 = 110

110 + 110 * 0.3 = 143

143 + 143×0.2 = 171.6

我有以下表格的“收入”和“比率”,我希望将查询结果作为“结果”。 我想我应该使用递归查询,但我不知道如何编写它 我正在使用SQL服务器

C++

2 个答案:

答案 0 :(得分:1)

如果月份没有间隙增加,可以通过加入month_n - 1来进行递归:

with
revenue as (
select 100 revenue 
)
,
ratios as
(
    select 1 month_n, 0.1 ratio
    union
    select 2 month_n, 0.3 ratio
    union
    select 3 month_n, 0.2 ratio
),
outcome as
(
    select (select MIN(month_n) from ratios) - 1 MONTH_n, 0.0 ratio, cast(revenue as float) result, 1 IsBase from revenue   
    union all
    select r.month_n , r.ratio, (r.ratio + 1.0) * o.result, 0
    from ratios r
    join outcome o on o.MONTH_n = r.month_n - 1
)
select MONTH_n,ratio,result  from outcome where IsBase = 0

结果:

MONTH_n ratio   result
1   0.1 110
2   0.3 143
3   0.2 171,6

但更安全的是使用row_number作为索引,特别是如果多年来进入图片。

作为一个完全不重要的旁注,你也可以在没有多余的“基础”行的情况下编写它,如下所示,但这会将计算定义放在两行中。这不会导致额外的计算,只是可维护性。

....,
outcome as
(
    select top 1 MONTH_n,  ratio, (ratio + 1.0) *  cast(revenue as float) result from revenue , ratios order by month_n
    union all
    select r.month_n , r.ratio, (r.ratio + 1.0) * o.result
    from ratios r
    join outcome o on o.MONTH_n = r.month_n - 1
)
select *  from outcome 

答案 1 :(得分:1)

如果您的比率非空,并且始终大于-1(因为您无论如何需要添加1来获取乘法因子)并且您还使用SQL Server 2012或更高版本,那么您的查询可以在没有递归的情况下得到解决:< / p>

WITH Revenue (Revenue) AS (SELECT 100),
Ratios AS (SELECT * FROM (VALUES (1, 0.1), (2, 0.3), (3, 0.2)) t (Month_n, Ratio))

SELECT  ra.month_n,
        ra.Ratio,
        CompoundRatio = EXP(SUM(LOG((1 + ra.Ratio))) OVER(ORDER BY Month_n)),
        Result = rv.Revenue * EXP(SUM(LOG((1 + ra.Ratio))) OVER(ORDER BY Month_n))
FROM    Ratios AS ra
        CROSS JOIN Revenue AS rv;

给出了:

month_n Ratio   Compound    Result
-------------------------------------
1       0.1     1.1         110
2       0.3     1.43        143
3       0.2     1.716       171.6

您可以使用窗口函数来总结以前的比率,以获得复合比率,但由于SQL Server没有产品聚合,您需要使用LOGEXP来获取产品以前的比率。

如果你的比率可以是负数和/或0,那么你仍然可以做到这一点,但你的逻辑变得有点复杂。有关检查执行情况的更多说明,请参阅this answer

WITH Revenue (Revenue) AS (SELECT 100),
Ratios AS (SELECT * FROM (VALUES (1, 0.1), (2, 0.3), (3, 0.2), (4, -0.1), (5, 0.2), (6, -0.2), (7, 0.05), (8, -1.1)) t (Month_n, Ratio))

SELECT  ra.month_n,
        ra.Ratio,
        CompoundRatio = CASE WHEN MIN(ABS(ra.Ratio + 1)) OVER(ORDER BY ra.Month_n) = 0 THEN 0
                            ELSE CASE WHEN SUM(CASE WHEN ra.Ratio < -1 THEN 1 ELSE 0 END) 
                                            OVER(ORDER BY ra.Month_n) % 2 = 1 THEN -1 ELSE 1 END *
                                EXP(SUM(LOG(ABS(NULLIF(1 + ra.Ratio, 0)))) OVER(ORDER BY Month_n))
                        END,
        Result = CASE WHEN MIN(ABS(ra.Ratio + 1)) OVER(ORDER BY ra.Month_n) = 0 THEN 0
                    ELSE CASE WHEN SUM(CASE WHEN ra.Ratio < -1 THEN 1 ELSE 0 END) 
                                    OVER(ORDER BY ra.Month_n) % 2 = 1 THEN -1 ELSE 1 END *
                        EXP(SUM(LOG(ABS(NULLIF(1 + ra.Ratio, 0)))) OVER(ORDER BY Month_n))
                END * rv.Revenue
FROM    Ratios AS ra
        CROSS JOIN Revenue AS rv;

结果(通过健全性检查):

month_n Ratio   CompoundRatio   SQLResult       Equation
------------------------------------------------------------------------
1       0.1     1.1             110             100 + 100 x 0.1 = 110
2       0.3     1.43            143             110 + 110 x 0.3 = 143
3       0.2     1.716           171.6           143 + 143 x 0.2 = 171.6
4       -0.1    1.5444          154.44          171.6 + 171.6 x -0.1 = 154.44
5       0.2     1.85328         185.328         154.44 + 154.44 x 0.2 = 185.328
6       -0.2    1.482624        148.2624        185.328 + 185.328 x -0.2 = 148.2624
7       0.05    1.5567552       155.67552       148.2624 + 148.2624 x 0.05 = 155.67552
8       -1.1    -0.15567552     -15.567552      155.67552 + 155.67552 x -1.1 = -15.567552