I'm trying to select a value based on a complicated expression from a data table, having a select like this where for future maintainability I'm not writing the whole expression in one, instead I'm calculating partial results in subselects :
(The query is not really important, the concept is, so please bear with me as I have depersonalized this sample query. I'm using one or two letter aliases just for presentation purposes)
select t.*,
(1 - nvl(min(t.O / t.MA) over(partition by t.fk_1, t.fk_2, t.fk_3, t.fk_4), 0)) *
t.MA as V --value regulated by overhead
from (select t.*,
decode(sign(t.MA - t.M), 1, t.MA - t.M, to_number(null)) as O --overhead
from (select t.*,
(1 - (max(t.PD) over(partition by t.fk_1, t.fk_2, t.fk_3, t.fk_4))) * t.ASZ as MA --value regulated by maximum percent difference
from (select t.*,
t.D / t.ASZ as PD --precent of diff
from (select t.*,
t.ASZ - t.BE as D --difference
from (select l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4,
l.fk_5,
l.BE, --Value that needs regulation by ratio on of Sum of values and maximum
l.A, --Ratio
l.SB, --Sum of values
l.M, --Maximum of value
decode((l.SB * l.A), 0, to_number(null), (l.SB * l.A)) as ASZ --Sum distributed as of given ratio
from vw_data l,
) t) t) t) t) t;
I was wondering if this could be rewritten in a way similar to this (which syntax doesn't work atm):
select l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4,
l.fk_5,
l.be, --Value that needs regulation by ratio on of Sum of values and maximum
l.a, --Ratio
l.sb, --Sum of values
l.m, --Maximum of value
decode((l.sb * l.a), 0, to_number(null), (l.sb * l.a)) as asz, --Sum distributed as of given ratio
asz - be as d, --difference
d / asz as pd, --precent of diff
(1 - (max(pd) over(partition by l.fk_1, l.fk_2, l.fk_3, l.fk_4))) * asz as ma, --value regulated by maximum percent difference
decode(sign(ma - l.m), 1, ma - l.m, to_number(null)) as o, --overhead
(1 - nvl(min(o / ma) over(partition by l.fk_1, l.fk_2, l.fk_3, l.fk_4), 0)) * ma as v --value regulated by overhead
from vw_data l
If you prefer to have it formatted a little bit better but much longer:
SELECT t.*,
( 1 - nvl( MIN( t.O / t.MA ) OVER ( PARTITION BY t.fk_1,
t.fk_2,
t.fk_3,
t.fk_4 ),
0 ) ) *
t.MA AS V --value regulated by overhead
FROM ( SELECT t.*,
DECODE( sign( t.MA - t.M ),
1,
t.MA - t.M,
TO_NUMBER( null ) ) AS O --overhead
FROM ( SELECT t.*,
( 1 - ( MAX( t.PD ) OVER ( PARTITION BY t.fk_1,
t.fk_2,
t.fk_3,
t.fk_4 ) ) ) *
t.ASZ AS MA --value regulated by maximum percent difference
FROM ( SELECT t.*,
t.D / t.ASZ AS PD --precent of diff
FROM ( SELECT t.*,
t.ASZ - t.BE AS D --difference
FROM ( SELECT l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4,
l.fk_5,
l.BE, --Value that needs regulation by ratio on of Sum of values and maximum
l.A, --Ratio
l.SB, --Sum of values
l.M, --Maximum of value
DECODE( ( l.SB * l.A ),
0,
TO_NUMBER( NULL ),
( l.SB * l.A ) ) AS ASZ --Sum distributed as of given ratio
FROM vw_data l,
) t ) t ) t ) t ) t;
I was wondering if this could be rewritten in a way similar to this (which syntax doesn't work atm):
SELECT l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4,
l.fk_5,
l.be, --Value that needs regulation by ratio on of Sum of values and maximum
l.a, --Ratio
l.sb, --Sum of values
l.m, --Maximum of value
DECODE( ( l.sb * l.a ),
0,
TO_NUMBER( NULL ),
( l.sb * l.a ) ) AS asz, --Sum distributed as of given ratio
asz - be AS d, --difference
d / asz AS pd, --precent of diff
( 1 - ( MAX( pd ) OVER ( PARTITION BY l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4 ) ) ) * asz AS ma, --value regulated by maximum percent difference
DECODE( SIGN ( ma - l.m ),
1,
ma - l.m,
TO_NUMBER( NULL ) ) AS o, --overhead
( 1 - nvl( MIN( o / ma ) OVER ( PARTITION BY l.fk_1,
l.fk_2,
l.fk_3,
l.fk_4 ), 0 ) ) * ma AS v --value regulated by overhead
FROM vw_data l