与此问题类似,但我的数据集还有一个包含许多ID的附加列。每个ID的数据集的返回时间都是固定的,有些星期可能会丢失数据- 我想填写缺少的几周的值。
例如,我想要这样:
ID WEEKEND_DAY VALUE A00 2012-01-01 1 A00 2012-01-08 7 B00 2012-01-08 4 B00 2012-01-15 3
扩展为:
ID WEEKEND_DAY VALUE A00 2012-01-01 1 A00 2012-01-08 7 A00 2012-01-15 0 B00 2012-01-01 0 B00 2012-01-08 4 B00 2012-01-15 3
我已经填写了我要填写的WEEKEND_DAY(在上述情况下,从2012年1月1日至2012年1月15日,每周)的已知范围。如何执行此操作?
使用分区外部联接:
select data.id, weeks.weekend_day, nvl(value, 0) value from ( select date '2012-01-01' weekend_day from dual union all select date '2012-01-08' weekend_day from dual union all select date '2012-01-15' weekend_day from dual ) weeks left join ( select 'A00' id, date '2012-01-01' weekend_day, 1 value from dual union all select 'A00' id, date '2012-01-08' weekend_day, 7 value from dual union all select 'B00' id, date '2012-01-08' weekend_day, 4 value from dual union all select 'B00' id, date '2012-01-15' weekend_day, 3 value from dual ) data partition by (data.id) on weeks.weekend_day = data.weekend_day