create table purchase(Customer,Item,Color,Num) as
select 'john','shirt','blue' ,2 from dual union
select 'john','shoes','black',1 from dual union
select 'mary','jeans','black',1 from dual union
select 'mary','jeans','blue' ,1 from dual union
select 'mary','shirt','blue' ,3 from dual union
select 'mary','shoes','blue' ,2 from dual;
--■■■Partitioned Outer Joinを使う方法(10g以降)■■■
select b.Customer,b.Item,a.Color,nvl(b.Num,0) as Num
from (select distinct Color from purchase) a
Left join purchase b
partition by (b.Customer,b.Item)
on (a.Color = b.Color)
order by b.Customer,b.Item,a.Color desc;
--■■■SQL99構文で結合させる方法■■■
select a.Customer,a.Item,b.Color,nvl(c.Num,0) as Num
from (select distinct Customer,Item from purchase) a
cross Join (select distinct Color from purchase) b
Left Join purchase c
on (a.Customer = c.Customer
and a.Item = c.Item
and b.Color = c.Color)
order by a.Customer,a.Item,b.Color desc;
--■■■グループ化する方法■■■
select b.Customer,b.Item,a.Color,
sum(case when b.Color=a.Color
then b.Num else 0 end) as Num
from (select distinct Color from purchase) a,purchase b
group by b.Customer,b.Item,a.Color
order by b.Customer,b.Item,a.Color desc;
--■■■grouping setsを使う方法■■■
select customer,item,
case when g_blue = 1 then 'blue'
when g_black = 1 then 'black' end as Color,num
from (select customer,item,
grouping('blue') as g_blue,
grouping('black') as g_black,
case when grouping('blue') = 1
then sum(decode(Color,'blue',num,0))
when grouping('black') = 1
then sum(decode(Color,'black',num,0)) end as num
from purchase
group by grouping sets((customer,item,'blue'),(customer,item,'black')))
order by customer,item,Color desc;