在 CONNECT-BY 查询中未获得全系列乘法(乘积)
Not getting full series multiplication (product) in CONNECT-BY query
我有一个 CONNECT BY 查询,但我没有从中获得所需的结果。
最终的子查询和查询应该生成百分比字符串相乘的乘积。
它从每年的 table 保留百分比 (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
(current/wrong 个结果)
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。更新后的查询做了顺序累积产品,我的要求是 "diagonally sequential cumulative products"。我保留了日志添加解决方案并添加了 CONNECT BY。
http://sqlfiddle.com/#!4/1c326/2
显示起点和深度的一些默认值。
再次感谢!
你需要的是累加乘法。但是没有聚合或分析函数这样的函数。但是数学告诉我们乘法
可以改成addition using logarithm.
a * b = exp(ln(a) + ln(b))
在 SUM 中使用它作为分析函数。无需使用 CONNECT BY 构造。
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 |
我有一个 CONNECT BY 查询,但我没有从中获得所需的结果。
最终的子查询和查询应该生成百分比字符串相乘的乘积。
它从每年的 table 保留百分比 (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
(current/wrong 个结果)
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。更新后的查询做了顺序累积产品,我的要求是 "diagonally sequential cumulative products"。我保留了日志添加解决方案并添加了 CONNECT BY。
http://sqlfiddle.com/#!4/1c326/2
显示起点和深度的一些默认值。
再次感谢!
你需要的是累加乘法。但是没有聚合或分析函数这样的函数。但是数学告诉我们乘法 可以改成addition using logarithm.
a * b = exp(ln(a) + ln(b))
在 SUM 中使用它作为分析函数。无需使用 CONNECT BY 构造。
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 |