create table testTable(Code1,Code2,Val) as
select 111,123,'AAA' from dual union
select 111,123,'BBB' from dual union
select 111,123,'CCC' from dual union
select 222,123,'AAA' from dual union
select 222,123,'DDD' from dual union
select 222,789,'BBB' from dual union
select 333,789,'CCC' from dual;
col Val for a10
--■■■Partitioned Outer Joinを使う方法(onで結合)(10g以降)■■■
select b.Code1,b.Code2,b.Val
from (select 1 as Counter from dual union all
select 2 from dual union all
select 3 from dual) a
Left outer Join (select Code1,Code2,Val,
Row_Number() over(partition by Code1,Code2 order by Val) as Rn
from testTable) b
partition by (b.Code1,b.Code2)
on (a.Counter = b.Rn)
order by b.Code1,b.Code2,b.Val;
--■■■Partitioned Outer Joinを使う方法(usingで結合)(10g以降)■■■
select b.Code1,b.Code2,b.Val
from (select 1 as JoinKey from dual union all
select 2 from dual union all
select 3 from dual) a
Left outer Join (select Code1,Code2,Val,
Row_Number() over(partition by Code1,Code2 order by Val) as JoinKey
from testTable) b
partition by (b.Code1,b.Code2)
using(JoinKey)
order by b.Code1,b.Code2,b.Val;
--■■■クロスジョインを使う方法■■■
select code1,code2,Val
from (select b.code1,b.code2,decode(a.Counter,1,b.Val) as Val,
Row_Number() over(partition by b.code1,b.code2 order by Counter) as Rn
from (select 1 as Counter from dual union all
select 2 from dual union all
select 3 from dual) a,testTable b)
where rn <= 3
order by code1,code2,Val;
--■■■model句を使う方法(10g以降)■■■
select Code1,Code2,Val
from testTable
model
partition by (Code1,Code2)
dimension by (Row_Number() over(partition by Code1,Code2 order by Val) as Rn)
measures(Val)
rules(Val[for Rn from 1 to 3 INCREMENT 1] = Val[cv()])
order by Code1,Code2,Rn;
--下記では、ダメなことに注意
--model句でのpartition byとは別なのです。
select Code1,Code2,Val
from testTable
model
partition by (Code1,Code2)
dimension by (Row_Number() over(/*partition by Code1,Code2*/ order by Val) as Rn)
measures(Val)
rules(Val[for Rn from 1 to 3 INCREMENT 1] = Val[cv()])
order by Code1,Code2,Rn;