--■■■階層問い合わせを使う方法■■■
col a for a5
col b for a5
col c for a5
col d for a5
col e for a5
col f for a5
col g for a5
with WorkView as
(select 1 as Val from dual union select 2 from dual union select 3 from dual
union select 4 from dual union select 5 from dual union select 6 from dual
union select 7 from dual),
重複組み合わせリスト as
(select substr(sys_connect_by_path(Val,','),2) as 組み合わせ
from WorkView
where Level = 7
connect by Level <= 7),
組み合わせリスト as
(select 組み合わせ from 重複組み合わせリスト a
where not exists(select 1 from WorkView b
where instr(a.組み合わせ,to_char(b.Val)) = 0)),
数値リスト as
(select RegExp_Replace(組み合わせ,'^([0-9]+).*$','\1') as a,
RegExp_Replace(組み合わせ,'^([0-9]+,)([0-9]+).*$','\2') as b,
RegExp_Replace(組み合わせ,'^([0-9]+,){2}([0-9]+).*$','\2') as c,
RegExp_Replace(組み合わせ,'^([0-9]+,){3}([0-9]+).*$','\2') as d,
RegExp_Replace(組み合わせ,'^([0-9]+,){4}([0-9]+).*$','\2') as e,
RegExp_Replace(組み合わせ,'^([0-9]+,){5}([0-9]+).*$','\2') as f,
RegExp_Replace(組み合わせ,'^([0-9]+,){6}([0-9]+).*$','\2') as g
from 組み合わせリスト)
select a,b,c,d,e,f,g
from 数値リスト
where to_number(a)+to_number(c) = to_number(b)
and to_number(c)+to_number(e) = to_number(d)
and to_number(e)+to_number(g) = to_number(f);
--■■■階層問い合わせを使わない方法■■■
col a for 999
col b for 999
col c for 999
col d for 999
col e for 999
col f for 999
col g for 999
select a,b,c,d,e,f,g
from (select
a,a+c as b,
c,c+e as d,
e,e+g as f,g
from (select RowNum as a from dict where RowNum <= 7),
(select RowNum as c from dict where RowNum <= 7),
(select RowNum as e from dict where RowNum <= 7),
(select RowNum as g from dict where RowNum <= 7))
where 1 in(a,b,c,d,e,f,g)
and 2 in(a,b,c,d,e,f,g)
and 3 in(a,b,c,d,e,f,g)
and 4 in(a,b,c,d,e,f,g)
and 5 in(a,b,c,d,e,f,g)
and 6 in(a,b,c,d,e,f,g)
and 7 in(a,b,c,d,e,f,g);