With my limited understanding of the model clause, I’d expected the following three queries to return the same results because there are no nulls in the data, and the functions are equivalent:
select *
from (select level k, 100 v from dual connect by level<=2)
model return updated rows
dimension by (k)
measures (v, 0 shr)
rules ( shr[any] = v[cv()]/sum(v)[any] ); --plain sum
/*
K V SHR
---------- ---------- ----------
1 100 0.5
2 100 0.5
*/
select *
from (select level k, 100 v from dual connect by level<=2)
model return updated rows
dimension by (k)
measures (v, 0 shr)
rules ( shr[any] = v[cv()]/nullif(sum(v)[any],0) ); --with nullif
/*
K V SHR
---------- ---------- ----------
1 100 0.25 <------___ why?
2 100 0.25 <------/
*/
select *
from (select level k, 100 v from dual connect by level<=2)
model return updated rows
dimension by (k)
measures (v, 0 shr)
rules ( shr[any] = v[cv()]/decode(sum(v)[any],0,null,sum(v)[any]) ); -- with decode
/*
K V SHR
---------- ---------- ----------
1 100 0.5
2 100 0.5
*/
What am I missing about the processing of rules that explains this behaviour?
SQLFiddle here