2016-07-31 4 views
0

DB2で再帰テーブル関数を作成する際に問題が発生しています。DB2再帰UDFテーブル

私はUDF表に渡す必要

With t (Adjustment) as (
VALUES (100) 
    , (200) 
    , (300) 
) select * from t; 

は、開口値(5000と言う)と係数(例えば0.1)機能....値のテーブルを持っている

Iが必要次のように....

Opening Shift Adjustment Closing 
(3)  (1)  (2) 
================================================== 
5000 500  100  5600    
5600 560  200  6360 
6360 636  300  7296 
  1. シフトの結果を示す=オープニング* 0.1
  2. 閉会=オープニング+
  3. オープニングは、前の行の閉鎖

私は前倒しを持参しようとしたときに私の再帰関数が立ち往生...(SQL0345N再帰共通テーブル式の全選択をあるを閉じるSHIFT + )

これを行う方法を理解してください。私はストアドプロシージャがこれを行うことができますが、別のUDFで拡張できるように、UDFを使用する必要があることを認識しています。

+0

テーブルT内のデータの照合を定義して、前の行_おそらく特定の順序なしで、row_number()over()?また、OPの2)は「Opening + Shift + Closing」と定義されていますが、その結果に応じて、式は「Opening + Shift + Adjustment」である必要があります。 – CRPence

答えて

1

私はきちんとシナリオを解釈するが、ここで私がやったことであればわからない午前:

開始するには、私は(関数への入力を模倣するカップルの変数を作成し、再帰共通テーブル式を構成します

create variable OpenedWith decimal  default 5000  
; 
create variable Factor  decimal(2, 1) default 0.1 
; 


with               
    t (adjustment) as (values(100), (200), (300))    
, ordRows (rn, Adjustment) as         
    (select row_number() over(), Adjustment from t)   
, addRows (rn, Opening, Shift, Adjustment, closing) as   
    (select rn, OpenedWith, OpenedWith*Factor , Adjustment  
      , (OpenedWith + (OpenedWith*Factor) + Adjustment) 
     from ordRows            
     where rn = 1            
    union all             
     select b.rn, a.Closing, a.Closing * Factor , b.Adjustment 
      , (a.Closing + (a.Closing * Factor) + b.Adjustment) 
     from addRows a            
     join ordRows b            
     on a.rn = (b.rn - 1)         
    )               
select int(rn) as rn, int(opening) as opening 
     , int(shift) as shift, adjustment  
     , int(closing) as closing    
from addRows         

:これらの変数から得られたデータを使用してレポートを生成するRCTE) 0 次は、上記のクエリからのレポートです:

:Tという名前のテーブル内のデータに対して操作するユーザ定義のテーブル・ファンクション(UDTF)に上記のスクリプト変数の作品やクエリを変更する

RN   OPENING   SHIFT  ADJUSTMENT   CLOSING 
    1   5,000    500    100   5,600 
    2   5,600    560    200   6,360 
    3   6,360    636    300   7,296 

そして今、

create function shifted_vals 
    (OpenedWith decimal(5)  
    , Factor  decimal(3, 2) 
)        
    returns table     
    (Opening  int   
    , Shift  int   
    , Adjustment int   
    , Closing  int   
)        
return       
with       
    ordRows (rn, Adjustment) as 
    (select row_number() over(), Adjustment from t)   
, addRows (rn, Opening, Shift, Adjustment, closing) as   
    (select rn, OpenedWith, OpenedWith*Factor , Adjustment  
      , (OpenedWith + (OpenedWith*Factor) + Adjustment) 
     from ordRows            
     where rn = 1            
    union all             
     select b.rn, a.Closing, a.Closing * Factor , b.Adjustment 
      , (a.Closing + (a.Closing * Factor) + b.Adjustment) 
     from addRows a            
     join ordRows b            
     on a.rn = (b.rn - 1)         
    )               
select opening, shift, adjustment, closing      
from addRows             
order by rn              


次に引数として注目開度値と係数とUDTFを呼び出します。すなわち、もはや作成された変数に依存しないで、入力パラメータを介して得られた値である。

select t.*        
from table(shifted_vals(5000, 0.1)) as t 
; -- results as report, follows: 
    OPENING   SHIFT  ADJUSTMENT   CLOSING 
    5,000    500    100   5,600 
    5,600    560    200   6,360 
    6,360    636    300   7,296 
+0

Thanks CRPence SQL0345N再帰的共通表式 " .ADDROWS"のfullselectは、複数の全選択のUNIONでなければならず、列関数、GROUP BY句、 HAVING句、ORDER BY句、またはON句を含む明示的な結合です。 –

+0

それは動作します! CRPence ON句を削除するだけです... 組合全て選択b.rn、a.Closing、a.Closing * 0.1、b.Adjustment、(a.Closing +(a.Closing * 0.1)+ b.Adjustment) ここで、b AddRowsを、ordRowsから a.rn =(b.rn-1) –

+0

ああ! DB2の変種のニュアンス、どのタグが使用されているかを明確にするタグがない。私はどちらが使用されているかを指摘しておきました。表示されたJOIN構文は、それらの照会が実行されたDB2 for i SQL上で受け入れられ、表示された出力はIBM i 7.1上で生成されました。私は答えを編集する]、私はこれらのコメントが誰かがエラーに遭遇するのを助ける役割を果たすことを許可する。 – CRPence