我有一个 CONNECT BY 查询,但没有得到所需的结果。
最终的子查询和查询应该产生一串百分比相乘的乘积。
它首先列出了每年的保留百分比 (PCT)。随着查询的进行,它应该将第一年的 PCT 乘以下一年的 PCT,依此类推,直到达到所需的深度。效果应该类似于交叉表中的对角线相乘,其中 YR(年份)和 yset 代表起始行和列。
我只得到最后两个百分比的结果,而不是随着查询的进行获取完整的字符串。
这可能是一个简单的错误,但更多的关注将有助于更快地发现它。
Code:
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 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
为什么我没有得到完整的(乘法)乘积?我可以做什么来解决这个问题?任何人...?任何人?布勒?
更新:吃一个桃子提供了获得真正累积结果所需的修复。我必须更新该示例,因为我手工定制的示例数据隐藏了常规数据的范围:连续 50 多年的行,每年最多 70 YCS。更新后的查询执行了顺序累积产品,我的要求是“对角顺序累积产品”。我保留了日志添加解决方案并将 CONNECT BY 添加回来。
http://sqlfiddle.com/#!4/1c326/2 http://sqlfiddle.com/#!4/1c326/2
显示起点和深度的一些默认值。
再次感谢!