我有一个 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
显示起点和深度的一些默认值。
再次感谢!