2016-07-11 11 views
0

圧縮ロジックによるデータ量を減らすために使用されるRDW(Retake Data Warehouse)。圧縮とは、基礎となるデータソースの変更のみを反映する物理データを格納することです。圧縮解除用SQLロジック

インベントリファクトテーブルは、次の形式でデータを格納します。

Week  Item  Location stock_on_hand 
-------------------------------------------------- 
201601  I1   L1   50 
201602  I1   L1   30 
201605  I1   L1   60 
201608  I1   L1   50 

しかし、私は(私は理解とスプリット年と週の列の複数の行を追加します)その結果、次の

Week  Item  Location stock_on_hand 
-------------------------------------------------- 
201601  I1   L1   50 
201602  I1   L1   30 
201603  I1   L1   30 
201604  I1   L1   30 
201605  I1   L1   60 
201606  I1   L1   60 
201607  I1   L1   60 
201608  I1   L1   50 

答えて

1

テストデータを取得するために、SQLクエリを必要とする

with t(year, Week , Item, Location, stock_on_hand) as 
(select 2016, 01,  'I1',   'L1',   50 from dual union all 
select 2016, 02,  'I1',   'L1',   30 from dual union all 
select 2016 ,05,  'I1',   'L1',   60 from dual union all 
select 2016 ,08,  'I1',   'L1',   50 from dual union all 
select 2016, 02,  'I2',   'L1',   30 from dual union all 
select 2016, 08,  'I2',   'L1',   40 from dual union all 
select 2016, 02,  'I1',   'L2',   10 from dual union all 
select 2016, 08,  'I1',   'L2',   40 from dual union all 
select 2016, 08,  'I1',   'L3',   40 from dual) 

クエリ

with t(year, Week , Item, Location, stock_on_hand) as 
(select 2016, 01,  'I1',   'L1',   50 from dual union all 
select 2016, 02,  'I1',   'L1',   30 from dual union all 
select 2016 ,05,  'I1',   'L1',   60 from dual union all 
select 2016 ,08,  'I1',   'L1',   50 from dual union all 
select 2016, 02,  'I2',   'L1',   30 from dual union all 
select 2016, 08,  'I2',   'L1',   40 from dual union all 
select 2016, 02,  'I1',   'L2',   10 from dual union all 
select 2016, 08,  'I1',   'L2',   40 from dual union all 
select 2016, 08,  'I1',   'L3',   40 from dual), 
temp(year, Week , Item, Location, stock_on_hand, ct) as(
select year, Week , Item, Location, stock_on_hand, nvl(lead(Week) over(partition by Item, Location order by year, Week)-Week,1) from t) 
select year, Week + rn - 1 as week, Item, Location, stock_on_hand 
    from temp, xmltable('1 to xs:integer($ct)' passing ct as "ct" columns rn number path '.') 
    order by Item, Location ,year, week 

このappr oachにも1つのマイナーがあります。別の年に間隔がある場合。 Ex

select 2016, 01,  'I1',   'L1',   50 from dual union all 
select 2017, 02,  'I1',   'L1',   30 from dual union all 

次に正しく動作しません。私はあなたのデータが同じパターンを持っているかどうかはわかりません。それがあれば、投稿または回答する情報を追加してください。数年に住んでいる間隔については

UPDATE あなたがフォローを行うことができます(たstartDateのために私は国際週の日付を選択します)

with t(dateStart , Item, Location, stock_on_hand) as 
(select to_date('28/12/2015', 'dd-mm-yyyy'),  'I1',   'L1',   50 from dual union all 
select to_date('04/01/2016', 'dd-mm-yyyy'),  'I1',   'L1',   30 from dual union all 
select to_date('25/01/2016', 'dd-mm-yyyy'),  'I1',   'L1',   60 from dual union all 
select to_date('15/02/2016', 'dd-mm-yyyy'),  'I1',   'L1',   50 from dual union all 
select to_date('01/01/2018', 'dd-mm-yyyy'),  'I1',   'L1',   30 from dual union all 
select to_date('04/01/2016', 'dd-mm-yyyy'),  'I2',   'L1',   40 from dual union all 
select to_date('15/02/2016', 'dd-mm-yyyy'),  'I2',   'L1',   10 from dual union all 
select to_date('04/01/2016', 'dd-mm-yyyy'),  'I1',   'L2',   30 from dual union all 
select to_date('15/02/2016', 'dd-mm-yyyy'),  'I1',   'L2',   40 from dual union all 
select to_date('15/02/2016', 'dd-mm-yyyy'),  'I1',   'L3',   40 from dual), 
temp(dateStart, Item, Location, stock_on_hand, ct) as(
select dateStart , Item, Location, stock_on_hand, nvl((lead(dateStart) over(partition by Item, Location order by dateStart)-dateStart)/7,1) from t) 
select dateStart + (rn - 1)*7 as week, Item, Location, stock_on_hand 
    from temp, xmltable('1 to xs:integer($ct)' passing ct as "ct" columns rn number path '.') 
    order by Item, Location , dateStart 
関連する問題