RDW(重新数据仓库)用于通过压缩逻辑减少数据量。压缩是指存储仅反映基础数据源更改的物理数据。
清单事实表以下列形式存储数据。
Week Item Location stock_on_hand
--------------------------------------------------
201601 I1 L1 50
201602 I1 L1 30
201605 I1 L1 60
201608 I1 L1 50
但是我需要sql查询来获得以下结果
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
答案 0 :(得分:1)
测试数据(我添加了几行以便更好地理解并拆分年和周列)
grunt default-prod
查询
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
然后它工作不正确。我不知道你的数据是否具有相同的模式。如果有,请添加信息以发布或回复。
<强>更新强> 对于生活在几年内的间隔,你可以遵循(对于startDate我选择国际周的日期)
select 2016, 01, 'I1', 'L1', 50 from dual union all
select 2017, 02, 'I1', 'L1', 30 from dual union all