3

不确定这是否可能通过标准查询,但这是我希望我的数据的方式。

START    END    SETTLE    NEW_SETTLE    CORRECTION_FACT    STORED_SETTLE
1        2      120       NULL          1                  120
2        3      127       119           1.0084             128.0668
3        4      NULL      125           1.0245344          NULL

计算如下:

CORRECTION_FACT

基本上,如果 NEW SETTLE 为空或这是第 1 行,则校正因子始终为 1。如果行 > 1,则校正因子为PREV ROW STORED_SETTLE / NEW_SETTLE 例如。120/119

STORED_SETTLE

这将始终是CORRECTION_FACTOR* SETTLE。在插入下一行之前,该值是未知的,因此有时它会为 NULL。棘手的部分是,这取决于CORRECTION_FACTwhich 也是计算值并且CORRECTION_FACT取决于STORED_SETTLE.

就我所拥有的价值观而言,我拥有SETTLE, NEW_SETTLE, STARTENDCORRECTION_FACT并且STORED_SETTLE总是需要计算。

所以这里的问题是我可以使用某种递归查询或分析函数来做到这一点,还是我必须编写一个脚本来填充?

4

1 回答 1

4
with T1 as
  (select 1 strt, 2 en, 120  settle, null new_settle from dual union all
   select 2 strt, 3 en, 127  settle, 119  new_settle from dual union all
   select 3 strt, 4 en, null settle, 125  new_settle from dual
  )
select strt
     , en
     , settle
     , new_settle
     , correction_fact
     , stored_settle 
  from t1
  model
   dimension by(row_number() over (order by strt) RowNumber)
   measures(strt, en, settle, new_settle
          , cast(null as number) correction_fact
          , cast(null as number) stored_settle)
   rules automatic order
   (
     correction_fact[1]=1,
     correction_fact[RowNumber>1] = decode(new_settle[cv()], null,
                  1,stored_settle[cv()-1]/new_settle[cv()]),                           
     stored_settle[rownumber]=(correction_fact[cv()]*settle[cv()])
   );

结果:

  STRT         EN     SETTLE NEW_SETTLE CORRECTION_FACT STORED_SETTLE
---------- ---------- ---------- ---------- --------------- -------------
     1          2        120                          1           120
     2          3        127        119      1.00840336    128.067227
     3          4                   125      1.02453782
于 2012-09-06T18:59:13.300 回答