Oracle RDBMS 11gR2 – Solving a Sudoku using Recursive Subquery Factoring
Oracle Database 11g Release 2 introduces a new feature called Recursive Subquery Factoring. My collegue Lucas sees it as a substitute for Connect By based hierarchical querying, Oracle RDBMS 11gR2 – new style hierarchical querying using Recursive Subquery Factoring. When I first was thinking about a pratical use for this feature I couldn’t come up with anything, but on second thought:: solving Sudokus!
Say you have a sudoku like:
To solve this sudoku you first have to transforms this to a single string by appending all rows together:
Past this string into a Recursive Subquery, run it and you get a new string with your solved sudoku:
with x( s, ind ) as
( select sud, instr( sud, ' ' )
from ( select '53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79' sud from dual )
union all
select substr( s, 1, ind - 1 ) || z || substr( s, ind + 1 )
, instr( s, ' ', ind + 1 )
from x
, ( select to_char( rownum ) z
from dual
connect by rownum <= 9
) z
where ind > 0
and not exists ( select null
from ( select rownum lp
from dual
connect by rownum <= 9
)
where z = substr( s, trunc( ( ind - 1 ) / 9 ) * 9 + lp, 1 )
or z = substr( s, mod( ind - 1, 9 ) - 8 + lp * 9, 1 )
or z = substr( s, mod( trunc( ( ind - 1 ) / 3 ), 3 ) * 3
+ trunc( ( ind - 1 ) / 27 ) * 27 + lp
+ trunc( ( lp - 1 ) / 3 ) * 6
, 1 )
)
)
select s
from x
where ind = 0
/
This string can be transformed back to a nice display of the solution
So with Recursive Subquery Factoring you can solve your sudokus in 1 statement wich does fit on your screen, not something like in Solving a Sudoku with 1 SQL-statement: the Model-clause
Anton
Related posts:
- Oracle RDBMS 11gR2 – new style hierarchical querying using Recursive Subquery Factoring
- Solving a Sudoku with 1 SQL-statement: the Model-clause
- Solving a Sudoku with Collections
- Subquery Factoring in Oracle 11g
- Oracle RDBMS 11gR2 – goodbye Connect By or: the end of hierarchical querying as we know it
This entry was posted by Anton Scheffer on October 13, 2009 at 11:11 am, and is filed under Database, Devel. + PL/SQL tools, General, Oracle. Follow any responses to this post through RSS 2.0.Both comments and pings are currently closed.
-
Comments are closed
-
#4 written by Tonny 1 year ago
-
#7 written by Anton Scheffer 3 years ago
-
#8 written by C 3 years ago
It looks to me that what the recursive subquery does is “breadth-first search”, i.e., for each blank square in the sudoku, it considers all possibilities before moving on to the next blank square. That computes all solutions to the sudoku, but I’m wondering if that is a good way of solving a sudoku in practice because it generally suffices to find one solution.
-
#9 written by Anton Scheffer 3 years ago
-
#11 written by W. G. 3 years ago
Inspired by your example, I did the Tower of Hanoi:
with h(x, n, a, b, c) as (
select 0, 3, ‘A’, ‘B’, ‘C’ from dual
union all
select x+m*power(2,n), n-1, decode(m, 1, b, a)
, decode(m, 1, a, c), decode(m, 1, c, b)
from h, (select -1 m from dual union all select 1 from dual)
where n > 1
)
select ‘move disk ‘ || n || ‘ from ‘ || a || ‘ to ‘ || c solution
from h order by x
; -
#12 written by Anton Scheffer 3 years ago
-
#13 written by addintel 3 years ago
how ever this dosen’t resolve all the sudoku types ..maybe just the easy ones.. for a more detailed explanation check this sudoku solver that also contains some strategies used by sudoku experts. http://www.scanraid.com/sudoku.htm . For example i dunno how your algorithm would resolve the Intersection Removal Strategy called also the Pointing Pairs Strategy.
-
#16 written by Frank 3 years ago
-
#18 written by Anton Scheffer 3 years ago
-
Ported using T-SQL @ http://michaelhthomasblog.blogspot.com/2009/11/solving-sudoku-using-recursive-common.html
Thank you Anton for the solution.
-
Great work!
Your work has been ported to PostgreSQL as well: http://wiki.postgresql.org/wiki/Sudoku_puzzle
-
#21 written by Anton Scheffer 3 years ago
-
#23 written by Anton Scheffer 3 years ago
-
-
#33 written by chithanh 3 years ago
-
#34 written by chithanh 3 years ago
-
-
#40 written by Anton Scheffer 3 years ago
@Laurent: I’ve tried the also some other number generators:
select *
from table( sys.odcinumberlist( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) )
/select *
from ( with x( r ) as
( select 1 r from dual
union all
select r + 1
from x
where r < 9
)
select * from x
)
/But, being nearly 50, I stuck with the old fashioned way
-
- Comment Feed for this Post
- Oracle SQL – Finding free adjacent seats in an airplane, using Subquery Factoring, Analytical Functions (LISTAGG and LEAD), Outer Join, PIVOT and good old INSTR
- Oracle SQL – spotting combinations that occur and those that do not – demonstrating Analytical Functions, Outer Join and SubQuery Factoring
- Oracle SQL: Using subquery factoring in an INSERT statement
- OTN Yathra 2013 – Spreading the story of Oracle across India – (Half time)
- ADF DVT Speed Date: Present Metrics per Year, Quarter and Month using a zoom-enabled ADF DVT Resource Utilization Gantt and ADF BC
- Using TRUNC in SQL to get the first date in a period
- Read an Excel xlsx with PL/SQL
- ADF DVT: Visualizing valid periods using Project and Scheduling Gantt Charts
- Advanced SQL to find valid periods – juggling with outer joins, running totals and analytical functions
- The Very Very Latest in Database Development – slides from the Expertezed presentation



Great post. Now you can start in an tournament. Anton and his machine.