sql - In Oracle how do you perform a calculation depending on the previous calculated result -
i perform simple calculation follows
dataset
sr value_one result 1 null 0.99 2 1 0.99*1 = 0.99 3 0.75 0.99*0.75 = 0.7425 4 0.75 0.7425*0.75 = 0.556875 5 1 0.556875*1 = 0.556875 6 1 0.556875*1 = 0.556875 7 1 0.556875*1 = 0.556875 8 1 0.556875*1 = 0.556875 9 1 0.556875*1 = 0.556875 10 1 0.556875*1 = 0.556875
the results dependent on previous 1 sr >= 2 while starts 0.99.
looping while performing calculation. database oracle.
if wanted calculate running sum, easy using analytic functions:
with d ( select 1 sr, cast(null number) value_one dual union select 2 sr, 1 value_one dual union select 3 sr, 0.75 value_one dual union select 4 sr, 0.75 value_one dual union select 5 sr, 1 value_one dual union select 6 sr, 1 value_one dual union select 7 sr, 1 value_one dual union select 8 sr, 1 value_one dual union select 9 sr, 1 value_one dual union select 10 sr, 1 value_one dual ) select d.*, sum(nvl(value_one, 0.99)) on (order sr) d;
unfortunately, there no "product" aggregate function use here, have make detour using exp , ln (see https://stackoverflow.com/a/3912248/1230592):
with d ( select 1 sr, cast(null number) value_one dual union select 2 sr, 1 value_one dual union .. ) select d.*, nvl(exp (sum (ln (value_one)) on (order sr)), 1) * 0.99 d;
this should give desired results
Comments
Post a Comment