sql - 在 CONNECT-BY 查询中未获得完整的系列乘法(乘积)

标签 sql oracle connect-by

我有一个 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
;

我在 SQLFiddle 中设置了一个示例,地址为 http://sqlfiddle.com/#!4/ce945/1/0
该示例使用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

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

更新:Eat A Peach 提供了获得真正累积结果所需的修复。我必须更新该示例,因为我手工定制的示例数据隐藏了常规数据的范围:连续 50 多年的行,每年最多 70 YCS。更新后的查询执行了顺序累积产品,我的要求是“对角顺序累积产品”。我保留了日志添加解决方案并添加了 CONNECT BY。

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

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

再次感谢!

最佳答案

你需要的是累积乘法。但没有聚合函数或分析函数这样的函数。但数学告诉我们乘法 可以改为addition using logarithm .

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

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

SQL Fiddle

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

<强> Results :

|   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 |

关于sql - 在 CONNECT-BY 查询中未获得完整的系列乘法(乘积),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27955093/

相关文章:

performance - 如何在没有提示的情况下增加哈希连接、分组依据和排序依据的 Oracle CBO 成本估算

sql - oracle 9i 使用给定的 child 获得树的最高成员

sql - 根据条件在 Oracle 中动态重复行

sql - 获取Hive中每个单词的唯一字数

python - 如何使用另一个表中的数据更新 Python 中的 oracle 表

Sqlite 使用命令行

sql - 动态插入 -- ORA-00923 : FROM keyword not found where expected

oracle - 将 Oracle SQL 转换为 PostgreSQL

sql - Visual Studio 2012 数据库项目 - 文件路径配置

sql - DB2 SQL 错误 sqlcode=-104 sqlstate=42601