2

我有一个 CONNECT BY 查询,但没有得到想要的结果。

最终的子查询和查询应该产生将一串百分比相乘的乘积。

它从每年的保留百分比 (PCT) 表开始。随着查询的进行,它应该取第一年的 PCT 并乘以第二年的 PCT,依此类推,直到达到所需的深度。效果应该类似于交叉表中的对角线相乘,其中 YR(年份)和 yset 代表起始行和列。

随着查询的进行,我没有得到完整的字符串,我只得到最后两个百分比的结果。

这可能是一个简单的错误,但额外的眼睛将有助于更快地发现它。

代码:

     with recurreten as
(
select  YR, YSet, 
      rtnpct rtn_year, 
      level lvl, ' - ' s1,   
      rtnpct * nvl( prior rtnpct, 1)  rtnpct 
      --- Below here only for checking the paths
      , sys_connect_by_path( nvl(rtnpct, 1) , '/')  prodpath 
from Z_RETENTIONPCT
    connect by yr = prior yr+1 and yset  = prior yset+1
    start with YR = 1998  -- :StartYr
               and
               yset = 20  -- :StartYSet
)

-- final results
select yr, yset, 
      round(rtn_year * 100, 2 ) rtn_year,   
      lvl, -- years the Cumulative Continuation Rate is extended
      s1,  
      round(rtnpct, 2) CCR  
      --- Below here only for checking results
      , rtnpct CCRFull -- Extra digits, for math check
      , prodpath  -- Only used by us, to check the #'s feeding the CCR
from recurreten
where  lvl <= 10 -- :Depth     
order by yr, yset, lvl
;

我在http://sqlfiddle.com/#!4/ce945/1/0
的 SQLFiddle 中设置了一个示例 该示例使用 WITH 来设置一些虚拟数据。

结果示例将是:(期望的结果)

Year    Col     Reten_yr    Full prod    Full Prod Path
1998    20      0.84766     0.847660000  = 0.84766
1999    21      0.77941     0.660674681  = 0.84766 * 0.77941
2000    22      0.78659     0.519680097  = 0.84766 * 0.77941 * 0.78659
2001    23      0.76879     0.399524862  = 0.84766 * 0.77941 * 0.78659 * 0.76879

(当前/错误结果)

Year    Col     Reten_yr    wrong prod   Partial Path
1998    20      0.84766     0.847660000  = 0.84766
1999    21      0.77941     0.660674681  = 0.84766 * 0.77941
2000    22      0.78659     0.613076112  =           0.77941 * 0.78659
2001    23      0.76879     0.604722526  =                     0.78659 * 0.76879

为什么我没有得到完整的(乘法)乘积?我能做些什么来解决这个问题?任何人...?任何人?布勒?

更新:吃桃子提供了获得真正累积结果所需的修复。由于我手工定制了隐藏常规数据范围的样本数据,我不得不更新该示例:连续 50 年以上的行,每年最多 70 个 YCS。更新后的查询做了连续累积产品,我的要求是“对角连续累积产品”。我保留了日志添加解决方案并添加了 CONNECT BY 回来。

http://sqlfiddle.com/#!4/1c326/2

显示起点和深度的一些默认值。

再次感谢!

4

1 回答 1

4

你需要的是累积乘法。但是没有聚合或分析函数这样的函数。但是数学告诉我们可以使用对数将乘法更改为加法

a * b =  exp(ln(a) + ln(b))

在 SUM 中使用它作为分析函数。无需使用 CONNECT BY 构造。

SQL小提琴

recurreten as
(
select  YR, YSet, 
      rtnpct rtn_year, 
      round(exp(sum(ln(rtnpct)) over (order by yr, yset rows between unbounded preceding and current row)),2) ccr,
      exp(sum(ln(rtnpct)) over (order by yr, yset rows between unbounded preceding and current row)) ccrfull
from Z_RETENTIONPCT
)
select * from recurreten
order by yr, yset

结果

|   YR | YSET | RTN_YEAR |  CCR |        CCRFULL |
|------|------|----------|------|----------------|
| 1998 |   20 |  0.84766 | 0.85 |        0.84766 |
| 1999 |   21 |  0.77941 | 0.66 |   0.6606746806 |
| 2000 |   22 |  0.78659 | 0.52 | 0.519680097013 |
| 2001 |   23 |  0.76879 |  0.4 | 0.399524861783 |
| 2002 |   24 |  0.80952 | 0.32 |  0.32342336611 |
| 2003 |   25 |  0.76316 | 0.25 | 0.246823776081 |
| 2004 |   26 |  0.82425 |  0.2 | 0.203444497435 |
| 2005 |   27 |   0.6992 | 0.14 | 0.142248392606 |
| 2006 |   28 |  0.77071 | 0.11 | 0.109632258666 |
| 2007 |   29 |    0.702 | 0.08 | 0.076961845583 |
于 2015-01-15T01:41:47.263 回答