-- File: queens_4_solution_pg.sql -- Version: 1.2 -- Last Changed: 2017-01-02 -- by: Damir; https://www.damirsystems.com -- Project: Eight Queens -- Description: Part 4; Solution -- DB: PostgreSQL CREATE OR REPLACE VIEW solution AS WITH w AS ( -- search space; place by columns SELECT q1.SQUARE as S1 , q2.SQUARE as S2 , q3.SQUARE as S3 , q4.SQUARE as S4 , q5.SQUARE as S5 , q6.SQUARE as S6 , q7.SQUARE as S7 , q8.SQUARE as S8 FROM board1 as q1 JOIN board1 as q2 ON q2.CL = 'b' and q2.RW not in (q1.rw) and q2.RW_NO not BETWEEN (q1.RW_NO - 1) and (q1.RW_NO + 1) JOIN board1 as q3 ON q3.CL = 'c' and q3.RW not in (q1.RW, q2.rw) and q3.RW_NO not BETWEEN (q2.RW_NO - 1) and (q2.RW_NO + 1) JOIN board1 as q4 ON q4.CL = 'd' and q4.RW not in (q1.RW, q2.RW, q3.rw) and q4.RW_NO not BETWEEN (q3.RW_NO - 1) and (q3.RW_NO + 1) JOIN board1 as q5 ON q5.CL = 'e' and q5.RW not in (q1.RW, q2.RW, q3.RW, q4.rw) and q5.RW_NO not BETWEEN (q4.RW_NO - 1) and (q4.RW_NO + 1) JOIN board1 as q6 ON q6.CL = 'f' and q6.RW not in (q1.RW, q2.RW, q3.RW, q4.RW, q5.rw) and q6.RW_NO not BETWEEN (q5.RW_NO - 1) and (q5.RW_NO + 1) JOIN board1 as q7 ON q7.CL = 'g' and q7.RW not in (q1.RW, q2.RW, q3.RW, q4.RW, q5.RW, q6.rw) and q7.RW_NO not BETWEEN (q6.RW_NO - 1) and (q6.RW_NO + 1) JOIN board1 as q8 ON q8.CL = 'h' and q8.RW not in (q1.RW, q2.RW, q3.RW, q4.RW, q5.RW, q6.RW, q7.rw) and q8.RW_NO not BETWEEN (q7.RW_NO - 1) and (q7.RW_NO + 1) WHERE q1.CL = 'a' ) SELECT S1, S2, S3, S4, S5, S6, S7, S8 FROM w WHERE not exists ( -- placing queen 2 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s1 INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s2 ) and not exists ( -- placing queen 3 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s3 ) and not exists ( -- placing queen 4 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2, w.s3) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s4 ) and not exists ( -- placing queen 5 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2, w.s3, w.s4) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s5 ) and not exists ( -- placing queen 6 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2, w.s3, w.s4, w.s5) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s6 ) and not exists ( -- placing queen 7 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2, w.s3, w.s4, w.s5, w.s6) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s7 ) and not exists (-- placing queen 8 SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE in (w.s1, w.s2, w.s3, w.s4, w.s5, w.s6, w.s7) INTERSECT SELECT distinct e.ANCHOR, e.MOV_DIR FROM square_line as e WHERE e.SQUARE = w.s8 ) ; COMMENT ON VIEW solution IS ' [p_07] Placing queens on squares (S1), (S2), (S3), (S4), (S5), (S6), (S7), and (S8) is a solution to the puzzle. KEY {S1, S2, S3, S4, S5, S6, S7, S8} ';