在Oracle中是否可以进行前导计算? 我有一个表,其中第一行设置为100%。此后的每一行等于前一个百分比乘以一个因子,在本示例中为o.99978。 我只能得到第一行的答案,而不能一直滚动下去。 seq 3应该等于0.99956,依此类推。
SELECT SEQ,
(CASE WHEN SEQ = 1 THEN PERC LAG(PERC) OVER (ORDER BY SEQ) * FACTOR END)
PERC
FROM
(SELECT 1 SEQ, 0.99978 FACTOR, 1 PERC FROM DUAL UNION ALL
SELECT 2 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 3 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 4 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 5 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 6 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 7 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 8 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 9 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL UNION ALL
SELECT 10 SEQ, 0.99978 FACTOR, NULL PERC FROM DUAL);
答案 0 :(得分:2)
只需执行以下操作:
select seq, factor, power(factor,seq-1) as perc
from my_table
您的表格就像图片中的表格一样,只有前两列
此处是dbfiddle上的example
答案 1 :(得分:1)
Oracle中没有PRODUCT()
聚合/分析函数,但是您可以使用EXP(SUM(LN(value)))
获得相同的效果(有一些舍入错误),并且要获得累积乘积,可以使用{{ 1}}窗口分析函数。
这演示了如何在每行的值都不相同的情况下执行计算:
SUM( value ) OVER ( ORDER BY ... [RANGE|ROWS] BETWEEN ... )
输出:
SEQ | FACTOR | PERC --: | -----: | ----------------------------------------: 1 | .99978 | 1 2 | .99978 | .9997799999999999999999999999999999999907 3 | .99978 | .9995600483999999999999999999999999999915 4 | .99978 | .9993401451893519999999999999999999999783 5 | .99978 | .9991202903574103425599999999999999999725 6 | .99978 | .9989004838935317122846367999999999999711 7 | .999 | .9986807257870751353079341799039999999662 8 | .99978 | .9976820450612880601726262457240959999696 9 | .99978 | .9974625550113745767993882679500366988414 10 | .99978 | .9972431132492720743924924025310876907504
db <>提琴here