select * from (select level as A from Dual connect by level<=8) A cross join (select level as B from Dual connect by level<=8) B cross join (select level as C from Dual connect by level<=8) C cross join (select level as D from Dual connect by level<=8) D cross join (select level as E from Dual connect by level<=8) E cross join (select level as F from Dual connect by level<=8) F cross join (select level as G from Dual connect by level<=8) G cross join (select level as H from Dual connect by level<=8) H;
not x in (y,z)
. For horizontals, everything is simple: where not A in (B,C,D,E,F,G,H) and not B in (A,C,D,E,F,G,H) ... and not H in (A,B,C,D,E,F,G)
not A in (B)
= not B in (A)
, etc., we obtain: where not A in (B,C,D,E,F,G,H) and not B in (C,D,E,F,G,H) ... and not G in (H)
where not A in (B+1,C+2,D+3,E+4,F+5,G+6,H+7) and not B in (A-1,C+1,D+2,E+3,F+4,G+5,H+6) ... and not H in (A-7,B-6,C-5,D-4,E-3,F-2,G-1)
not A in (B+1)
= not B in (A-1)
remove unnecessary comparisons: where not A in (B+1,C+2,D+3,E+4,F+5,G+6,H+7) and not B in (C+1,D+2,E+3,F+4,G+5,H+6) ... and not G in (H+1)
where not A in (B-1,C-2,D-3,E-4,F-5,G-6,H-7) and not B in (C-1,D-2,E-3,F-4,G-5,H-6) ... and not G in (H-1)
where
I grouped all the conditions: where not A in (B,C,D,E,F,G,H,B+1,C+2,D+3,E+4,F+5,G+6,H+7,B-1,C-2,D-3,E-4,F-5,G-6,H-7) and not B in (C,D,E,F,G,H,C+1,D+2,E+3,F+4,G+5,H+6,C-1,D-2,E-3,F-4,G-5,H-6) and not C in (D,E,F,G,H,D+1,E+2,F+3,G+4,H+5,D-1,E-2,F-3,G-4,H-5) and not D in (E,F,G,H,E+1,F+2,G+3,H+4,E-1,F-2,G-3,H-4) and not E in (F,G,H,F+1,G+2,H+3,F-1,G-2,H-3) and not F in (G,H,G+1,H+2,G-1,H-2) and not G in (H,H+1,H-1)
select 'a' || AA, 'b' || BB, 'c' || CC, 'd' || DD, 'e' || EE, 'f' || FF, 'g' || GG, 'h' || HH from (select level as A from Dual connect by level<=8) A cross join (select level as B from Dual connect by level<=8) B cross join (select level as C from Dual connect by level<=8) C cross join (select level as D from Dual connect by level<=8) D cross join (select level as E from Dual connect by level<=8) E cross join (select level as F from Dual connect by level<=8) F cross join (select level as G from Dual connect by level<=8) G cross join (select level as H from Dual connect by level<=8) H where not A in (B,C,D,E,F,G,H,B+1,C+2,D+3,E+4,F+5,G+6,H+7,B-1,C-2,D-3,E-4,F-5,G-6,H-7) and not B in (C,D,E,F,G,H,C+1,D+2,E+3,F+4,G+5,H+6,C-1,D-2,E-3,F-4,G-5,H-6) and not C in (D,E,F,G,H,D+1,E+2,F+3,G+4,H+5,D-1,E-2,F-3,G-4,H-5) and not D in (E,F,G,H,E+1,F+2,G+3,H+4,E-1,F-2,G-3,H-4) and not E in (F,G,H,F+1,G+2,H+3,F-1,G-2,H-3) and not F in (G,H,G+1,H+2,G-1,H-2) and not G in (H,H+1,H-1) order by A, B, C, D, E, F, G, H;
Source: https://habr.com/ru/post/228373/
All Articles