トップページに戻る    次のSQLパズルへ    前のSQLパズルへ

11-6 連環の数 上級編

SQLパズル

連なった円の中に1から始まる、連続した自然数を入れていきます。
ただし、円の重なった部分には、その左右に入っている数の和を入れてください

円が8個の場合、1から15までの数を入れてみてください。


SQL

col a for 99
col b for 99
col c for 99
col d for 99
col e for 99
col f for 99
col g for 99
col h for 99
col i for 99
col j for 99
col k for 99
col l for 99
col m for 99
col n for 99
col o for 99

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 union select  8 from dual union select  9 from dual
union select  10 from dual union select 11 from dual union select 12 from dual
union select  13 from dual union select 14 from dual union select 15 from dual)
select a.Val as a,b.Val as b,c.Val as c,d.Val as d,e.Val as e,f.Val as f,
g.Val as g,h.Val as h,i.Val as i,j.Val as j,k.Val as k,l.Val as l,
m.Val as m,n.Val as n,o.Val as o
from
WorkView a,WorkView b,WorkView c,WorkView d,WorkView e,WorkView f,
WorkView g,WorkView h,WorkView i,WorkView j,WorkView k,WorkView l,
WorkView m,WorkView n,WorkView o
where a.Val not in(b.Val,c.Val,d.Val,e.Val,f.Val,g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and b.Val not in(c.Val,d.Val,e.Val,f.Val,g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and c.Val not in(d.Val,e.Val,f.Val,g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and d.Val not in(e.Val,f.Val,g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and e.Val not in(f.Val,g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and f.Val not in(g.Val,h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and g.Val not in(h.Val,i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and h.Val not in(i.Val,j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and i.Val not in(j.Val,k.Val,l.Val,m.Val,n.Val,o.Val)
  and j.Val not in(k.Val,l.Val,m.Val,n.Val,o.Val)
  and k.Val not in(l.Val,m.Val,n.Val,o.Val)
  and l.Val not in(m.Val,n.Val,o.Val)
  and m.Val not in(n.Val,o.Val)
  and n.Val != o.Val
  and b.Val = a.Val+c.Val
  and d.Val = c.Val+e.Val
  and f.Val = e.Val+g.Val
  and h.Val = g.Val+i.Val
  and j.Val = i.Val+k.Val
  and l.Val = k.Val+m.Val
  and n.Val = m.Val+o.Val;


解説

クロスジョインで組み合わせを列挙してます。