From: Johan Nel on
Hi all,

I know its a bit off-topic, but apparently there was a Oracle example put
on the web regarding solving a Sudoko solution. Somebody posted the same
example using PostgreSQL on the postgre news groups. Anybody know other
solutions or did something similar in VO? The below example when
replacing some of the digits from the input string will show all the
possible solutions for the Sudoko trivia. One of my colleagues at work
tried to get it working in MS-Sql, but he gave up. We did find a
solution, but it was using a stored procedure with cursors.

Not something one would normally do, but I thought it was quite some nifty
outside the box thinking, hence sharing it.

Regards,

Johan.

I include the statement:

with recursive x( s, ind ) as
( select sud, position( ' ' in sud )
from (select '53 7 '||
'6 195 '||
' 98 6 '||
'8 6 3'||
'4 8 3 1'||
'7 2 6'||
' 6 28 '||
' 419 5'||
' 8 79'::text as sud) xx
union all
select substr( s, 1, ind - 1 ) || z || substr( s, ind + 1 )
, position(' ' in repeat('x',ind) || substr( s, ind + 1 ) )
from x
, (select gs::text as z from generate_series(1,9) gs)z
where ind > 0
and not exists
( select null
from generate_series(1,9) lp
where z.z = substr( s, ( (ind - 1 ) / 9 ) * 9 + lp, 1 )
or z.z = substr( s, mod( ind - 1, 9 ) - 8 + lp * 9, 1 )
or z.z = substr( s, mod( ( ( ind - 1 ) / 3 ), 3 ) * 3
+ ( ( ind - 1 ) / 27 ) * 27 + lp
+ ( ( lp - 1 ) / 3 ) * 6
, 1 )
)
)
select s
from x
where ind = 0;
--
Total query runtime: 512 ms.
1 row retrieved.
s
-----------------------------------------------------------------------------------
"534678912672195348198342567859761423426853791713924856961537284287419635345286179"