create table SalesSlips(
style_A char(1) not null,
style_B char(1) not null,
style_C char(1) not null,
style_D char(1) not null,
tally number(3) not null);
insert into SalesSlips values('A','A','A','A', 2);
insert into SalesSlips values('A','A','B','B', 4);
insert into SalesSlips values('A','A','C','A', 8);
insert into SalesSlips values('A','B','A','B', 16);
insert into SalesSlips values('A','B','B','A', 30);
insert into SalesSlips values('A','B','C','B', 50);
insert into SalesSlips values('A','C','A','A', 70);
insert into SalesSlips values('A','C','B','B', 80);
insert into SalesSlips values('A','C','C','A',100);
insert into SalesSlips values('B','A','A','B',130);
insert into SalesSlips values('B','A','B','A',180);
insert into SalesSlips values('B','A','C','B',220);
insert into SalesSlips values('B','B','A','A',260);
insert into SalesSlips values('B','B','B','B',300);
insert into SalesSlips values('B','B','C','A',340);
insert into SalesSlips values('B','C','A','B',390);
insert into SalesSlips values('B','C','B','A',460);
insert into SalesSlips values('B','C','C','B',500);
insert into SalesSlips values('C','A','A','A',530);
insert into SalesSlips values('C','A','B','B',580);
insert into SalesSlips values('C','A','C','A',690);
insert into SalesSlips values('C','B','A','B',730);
insert into SalesSlips values('C','B','B','A',780);
insert into SalesSlips values('C','B','C','B',830);
commit;
--■■■場合分けを行い、単純case式を使う方法■■■
select style_A,style_B,style_C,style_D,sum(tally) as tally
from (select Least(style_A,style_B,style_C,style_D) as style_A,
case Least(style_A,style_B,style_C,style_D)
when style_A then Least(style_B,style_C,style_D)
when style_B then Least(style_A,style_C,style_D)
when style_C then Least(style_A,style_B,style_D)
when style_D then Least(style_A,style_B,style_C) end as style_B,
case greatest(style_A,style_B,style_C,style_D)
when style_A then greatest(style_B,style_C,style_D)
when style_B then greatest(style_A,style_C,style_D)
when style_C then greatest(style_A,style_B,style_D)
when style_D then greatest(style_A,style_B,style_C) end as style_C,
greatest(style_A,style_B,style_C,style_D) as style_D,
tally
from SalesSlips)
group by style_A,style_B,style_C,style_D
order by style_A,style_B,style_C,style_D;
--■■■unionしてRow_Number関数を使う方法■■■
select style_A,style_B,style_C,style_D,sum(tally) as tally
from (select
max(decode(rank,1,style)) as style_A,
max(decode(rank,2,style)) as style_B,
max(decode(rank,3,style)) as style_C,
max(decode(rank,4,style)) as style_D,
sum(tally) as tally
from (select Row_Number() over(partition by Row_ID order by style) as rank,
Row_ID,style,tally
from (select RowID as Row_ID,style_A as style,tally from SalesSlips
union all
select RowID,style_B,0 from SalesSlips
union all
select RowID,style_C,0 from SalesSlips
union all
select RowID,style_D,0 from SalesSlips))
group by Row_ID)
group by style_A,style_B,style_C,style_D
order by style_A,style_B,style_C,style_D;