create table CodeValTable(day1,Code,Val) as
select '20070101','a01',10 from dual union
select '20070101','a03', 5 from dual union
select '20070101','a04', 4 from dual union
select '20070101','a05',23 from dual union
select '20070101','a06', 3 from dual union
select '20070205','a01',25 from dual union
select '20070205','a02',32 from dual union
select '20070205','a03',42 from dual union
select '20070205','a04',33 from dual union
select '20070205','a05',52 from dual union
select '20070205','a06',11 from dual union
select '20070321','a01',98 from dual union
select '20070321','a03',76 from dual union
select '20070321','a05',54 from dual union
select '20070426','a02',32 from dual union
select '20070426','a04',10 from dual;
--■■■SQL99構文を使う方法■■■
select a.day1,b.Code,nvl(c.Val,0) as Val,
Row_Number() over(partition by a.day1 order by b.Code) as RNK
from (select distinct day1 from CodeValTable) a
cross join (select distinct Code from CodeValTable) b
Left Join CodeValTable c
on (c.day1 = a.day1
and c.Code = b.Code)
order by a.day1,b.Code;
--■■■Partitioned Outer Joinを使う方法(10g以降)■■■
select b.day1,a.Code,nvl(b.Val,0) as Val,
Row_Number() over(partition by b.day1 order by a.Code) as RNK
from (select distinct Code from CodeValTable) a
Left Join CodeValTable b
partition by (b.day1)
on (a.Code=b.Code)
order by b.day1,a.Code;
--■■■model句を使う方法(10g以降)■■■
select day1,code,Val,Row_Number() over(partition by day1 order by code) as RNK
from (select day1,code,Val
from CodeValTable
model
partition by (day1)
dimension by (code)
measures(Val)
rules(
Val[for code like 'a0%' from 1 to 6 INCREMENT 1] = nvl(Val[cv()],0)))
order by day1,code;