<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>OraQA</title>
	
	<link>http://oraqa.com</link>
	<description>Oracle Question and Answer</description>
	<lastBuildDate>Thu, 14 Jan 2010 06:47:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/OraQA" /><feedburner:info uri="oraqa" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><item>
		<title>How to solve the Optimal Ghost Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/p8zHr5ht11U/</link>
		<comments>http://oraqa.com/2010/01/13/how-to-solve-the-optimal-ghost-puzzle-in-sql/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 06:47:04 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=394</guid>
		<description><![CDATA[The following is an interesting puzzle posted by Itasofware on the web.
In the game of Ghost , two players take turns building up an English word from left to right. 
Each player adds one letter per turn. The goal is to not complete the spelling of a word: if you add a letter that completes [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting puzzle posted by <a href="http://www.itasoftware.com/careers/hiringpuzzles.html?catid=114">Itasofware</a> on the web.</p>
<p>In the game of <a href="http://www.kith.org/logos/words/lower/g.html">Ghost</a> , two players take turns building up an English word from left to right. </p>
<p>Each player adds one letter per turn. The goal is to not complete the spelling of a word: if you add a letter that completes a word (of 4+ letters), or if you add a letter that produces a string that cannot be extended into a word, you lose. </p>
<p>Write a program that allows a user to play Ghost against the computer. The computer should play optimally given the following dictionary: <a href="http://www.kith.org/logos/words/lower/g.html">WORD.LST</a>. </p>
<p>Allow the human to play first. If the computer thinks it will win, it should play randomly among all its winning moves; if the computer thinks it will lose, it should play so as to extend the game as long as possible (choosing randomly among choices that force the maximal game length). </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Here is the strategy for solving this problem: Try to add a letter to the current input string, so that there is only &#8220;one word&#8221; left in the whole dictionary for your opponent. Once your opponent adds the next letter to the input string, that will complete the spelling of the word.</p>
<p>Solution:<br />
Copy the word.lst from the web to a word.txt file on the database server ( \tmp directory). </p>
<pre>

create or replace directory tmp as 'C:\oracle\tmp';

CREATE  TABLE words (str VARCHAR2(38))
     ORGANIZATION EXTERNAL
     (
       TYPE ORACLE_LOADER
       DEFAULT DIRECTORY tmp
       ACCESS PARAMETERS
       (
         records delimited by newline
         fields terminated by whitespace
         missing field values are null
         ( str
         )
       )
       LOCATION ('word.txt')
     )
     REJECT LIMIT UNLIMITED;

COLUMN input FORMAT A18
COLUMN destination FORMAT A28
COLUMN next_char   FORMAT A12

variable input varchar2(38)
SQL&gt; exec :input :='tar'

PL/SQL procedure successfully completed.

------------------------------SQL Solution----------------- 

WITH data AS
(SELECT str, length(str) as len, :input as input, length(:input) as in_len,
        case when str like :input||'_'
             then  str
             end as flag_str
 FROM words
 WHERE str LIKE :input||'%' and length(str) &gt;=4
),
flag_data as
(
SELECT flag_str FROM data WHERE flag_str is not null
),
Filter_data as
(
SELECT distinct a.str FROM data a , flag_data b
WHERE substr(a.str, 1, length(b.flag_str)) = b.flag_str
),
Base_data as
(SELECT str, len , in_len, input FROM data
 WHERE str not in ( SELECT str FROM Filter_data)
)
SELECT input, destination, next_char
FROM
(SELECT input, str as destination, substr(str, in_len+1, 1) as next_char
  FROM
 (SELECT a.str, a.len, input, a.in_len,
         case when mod(a.len, 2) = 0 then 1
              else case when  REGEXP_LIKE (a.str, '^'||input||'[[:alpha:]]{2}$')
                        then case when
                                 (SELECT count(*) FROM data
                                  WHERE REGEXP_LIKE (str, '^'||substr(a.str,0,length(a.input)+1)||'[[:alpha:]]{2,}$')
                                  and  not REGEXP_LIKE (str, '^'||a.str||'[[:alpha:]]+$')
                                  ) = 0
                                  then 4
                                  else 3
                             end
                        else  2
                    end
         end as flag
  FROM Base_data a
 ) ORDER BY flag desc , case when flag = 1 then len else 0 end desc
)
WHERE ROWNUM = 1;

INPUT              DESTINATION                  NEXT_CHAR
------------------ ---------------------------- ------------
tar                targe                        g

SQL&gt; exec :input := 'bot'

SQL&gt; /

INPUT              DESTINATION                  NEXT_CHAR
------------------ ---------------------------- ------------
bot                botch                        c

SQL&gt; exec :input := 'aba'

SQL&gt; /

INPUT              DESTINATION                  NEXT_CHAR
------------------ ---------------------------- ------------
aba                abamp                        m
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/OraQA?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/OraQA?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/OraQA?i=p8zHr5ht11U:TRiYcUjMo64:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:ACf-c_HutVc"><img src="http://feeds.feedburner.com/~ff/OraQA?d=ACf-c_HutVc" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/OraQA?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=p8zHr5ht11U:TRiYcUjMo64:guobEISWfyQ"><img src="http://feeds.feedburner.com/~ff/OraQA?i=p8zHr5ht11U:TRiYcUjMo64:guobEISWfyQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/p8zHr5ht11U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2010/01/13/how-to-solve-the-optimal-ghost-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2010/01/13/how-to-solve-the-optimal-ghost-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to determine the table creation order in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/CklNa6n4eXM/</link>
		<comments>http://oraqa.com/2009/07/28/how-to-determine-the-table-creation-order-in-sql/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 05:55:52 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=340</guid>
		<description><![CDATA[The following SQL pattern can be used to determine the table creation order for a set of tables with parent-child relationship.
This SQL solution is built on top of a sql from the AskTom website, it assumes no circular dependency between the tables
create table p    ( x int primary key );
create table c1   ( x primary key [...]]]></description>
			<content:encoded><![CDATA[<p>The following SQL pattern can be used to determine the table creation order for a set of tables with parent-child relationship.<br />
This SQL solution is built on top of a sql from the <a href="http://asktom.oracle.com/pls/asktom/f?p=100:1:0">AskTom </a>website, it assumes no circular dependency between the tables</p>
<p>create table p    ( x int primary key );<br />
create table c1   ( x primary key references p );<br />
create table c2   ( x primary key references c1 );<br />
create table c3   ( x primary key references c2 );<br />
create table c4   ( x primary key references c2 );<br />
create table p_t  ( x int primary key );<br />
create table c_t  ( x primary key references p_t );<br />
create table c_t2 ( x primary key references c_t );<br />
create table c_t3 ( x primary key references c_t2 );<br />
create table c_t4 ( x primary key references c_t3 );<br />
create table c_t5 ( x primary key references c_t4 );<br />
create table m3   (x int, y int, z int );<br />
ALTER TABLE m3 ADD (CONSTRAINT x unique  (x) );<br />
ALTER TABLE m3 ADD (CONSTRAINT y unique  (y) );<br />
ALTER TABLE m3 ADD (CONSTRAINT z unique  (z) );<br />
ALTER TABLE m3 ADD (CONSTRAINT cx FOREIGN KEY (x) REFERENCES c_t5 (x));<br />
ALTER TABLE m3 ADD (CONSTRAINT cy FOREIGN KEY (y) REFERENCES c3 (x));<br />
ALTER TABLE m3 ADD (CONSTRAINT cz FOREIGN KEY (z) REFERENCES c4(x));</p>
<p><code>variable input varchar2(1028)<br />
exec :input := 'P,C1,C2,C3,C4,M3,P_T,C_T,C_T2,C_T3,C_T4,C_T5'</code><br />
 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<pre>With table_list AS
(
select doc.extract('/X/text( )').getStringVal() as t_name
from
TABLE(xmlSequence(extract(XMLType('&lt;DOC&gt;&lt;X&gt;'||
      REGEXP_REPLACE(:input, ',', '&lt;/X&gt;&lt;X&gt;')||'&lt;/X&gt;&lt;/DOC&gt;'),'/DOC/X'))) doc
)
select table_name, row_number( ) over (order by le) table_creation_order
from
(select distinct table_name, le
 from
 (select le, max(le) over (partition by table_name) max_level, table_name
  from
  (select table_name, level le
   from
   (select table_name, constraint_name pkey,
           to_char(null) fkey, to_char(null) r_con
    from   user_constraints
    where  constraint_type in ('U', 'P')
    and    owner = USER
    and table_name in (select  t_name from table_list)
    union all
    select a.table_name,
           a.constraint_name pkey,
           b.constraint_name fkey,
           b.r_constraint_name r_con
    from user_constraints a, user_constraints b
    where a.table_name = b.table_name
      and a.constraint_type in ('U', 'P')
      and b.constraint_type = 'R'
      and a.owner = USER
      and b.owner = a.owner
      and a.table_name in (select  t_name from table_list)
  )
  start with fkey is null
  connect by nocycle prior pkey = r_con
  order SIBLINGS by table_name DESC
  )
  )
  where le = max_level
);</pre>
<p>TABLE_NAME                                               TABLE_CREATION_ORDER</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;                                                  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>P                                                                                  1</p>
<p>P_T                                                                            2</p>
<p>C1                                                                               3</p>
<p>C_T                                                                            4</p>
<p>C2                                                                              5</p>
<p>C_T2                                                                         6</p>
<p>C_T3                                                                         7</p>
<p>C3                                                                              8</p>
<p>C4                                                                              9</p>
<p>C_T4                                                                        10</p>
<p>C_T5                                                                        11</p>
<p>M3                                                                            12</p>
<p>12 rows selected.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/OraQA?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/OraQA?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/OraQA?i=CklNa6n4eXM:Qb2fOFRNuD8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:ACf-c_HutVc"><img src="http://feeds.feedburner.com/~ff/OraQA?d=ACf-c_HutVc" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/OraQA?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/OraQA?a=CklNa6n4eXM:Qb2fOFRNuD8:guobEISWfyQ"><img src="http://feeds.feedburner.com/~ff/OraQA?i=CklNa6n4eXM:Qb2fOFRNuD8:guobEISWfyQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/CklNa6n4eXM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2009/07/28/how-to-determine-the-table-creation-order-in-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://oraqa.com/2009/07/28/how-to-determine-the-table-creation-order-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the Largest 7-Digit Number Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/urTi-mDwcLA/</link>
		<comments>http://oraqa.com/2009/01/18/how-to-solve-the-largest-7-digit-number-puzzle-in-sql/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 00:07:31 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=327</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
Work out the largest 7-digit number you can applying 2 rules only:
1) every digit in the number must be able to be divided into the number;
2) no digit can be repeated.
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;

SELECT  str_num
FROM
(SELECT str_num
  FROM
  (SELECT str_num, to_number(substr(str_num,LEVEL,1)) n1
   FROM
  [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>Work out the largest 7-digit number you can applying 2 rules only:</p>
<p>1) every digit in the number must be able to be divided into the number;<br />
2) no digit can be repeated.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<pre>
SELECT  str_num
FROM
(SELECT str_num
  FROM
  (SELECT str_num, to_number(substr(str_num,LEVEL,1)) n1
   FROM
   (SELECT to_number(utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw(
           replace(sys_connect_by_path(n,','),',')))))  str_num
    FROM (SELECT LEVEL n FROM dual CONNECT BY LEVEL &lt;10)
    WHERE LEVEL = 7
    CONNECT BY NOCYCLE PRIOR n != n
    AND LEVEL &lt; 8
    AND CASE LEVEL
        WHEN 2
	THEN CASE WHEN n in (2, 6, 8)
	          THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n in (4)
		  THEN CASE WHEN MOD(to_number(PRIOR N||N), n) = 0 THEN 1 END
		  WHEN n = 5
		  THEN CASE WHEN PRIOR N in (5) THEN 1 END
		  ELSE 1 END
	WHEN 3
	THEN CASE WHEN n in (2, 6)
		  THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n in (4)
		  THEN CASE WHEN MOD(to_number(PRIOR N||N), n) = 0 THEN 1 END
		  WHEN n in (8)
		  THEN CASE WHEN MOD(to_number(CONNECT_BY_ROOT(n)||PRIOR N||N),n)= 0
			    THEN 1 END
	          WHEN n = 5
		  THEN CASE WHEN CONNECT_BY_ROOT(n) in (5) THEN 1 END
		  ELSE 1 END
        WHEN 4
        THEN CASE WHEN n in (2, 4, 6 ,8)
		  THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n = 5
                  THEN CASE WHEN CONNECT_BY_ROOT(n) in (5) THEN 1 END
		  ELSE 1 END
	WHEN 5
        THEN CASE WHEN n in (2, 4, 6 ,8)
		  THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n = 5
	          THEN CASE WHEN CONNECT_BY_ROOT(n) in (5 ) THEN 1 END
		  ELSE 1 END
	WHEN 6
        THEN CASE WHEN n in (2, 4, 6 ,8)
		  THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n = 5
                  THEN CASE WHEN CONNECT_BY_ROOT(n) in (5) THEN 1 END
		  ELSE 1 END
        WHEN 7
        THEN CASE WHEN n in (2, 4, 6 ,8)
		  THEN CASE WHEN MOD(CONNECT_BY_ROOT(n), 2) = 0 THEN 1 END
		  WHEN n = 5
		  THEN CASE WHEN CONNECT_BY_ROOT(n) in (5) THEN 1 END
		  ELSE 1 END
        ELSE 1 END = 1
      )
      CONNECT BY PRIOR str_num = str_num
      AND LEVEL &lt;= length(str_num)
      AND PRIOR DBMS_RANDOM.STRING ('P',20) IS NOT NULL
   )
   GROUP BY str_num
   HAVING count(CASE WHEN mod(str_num, n1) = 0
                     THEN 1 END ) = length(str_num)
   ORDER BY str_num DESC
)
WHERE ROWNUM  = 1;
</pre>
<p>   STR_NUM<br />
&#8212;&#8212;&#8212;-<br />
   9867312</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=1eIMTnYo"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=QVeAOrdO"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=eKIVnZ9U"><img src="http://feeds.feedburner.com/~f/OraQA?i=eKIVnZ9U" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=nqU1imRx"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=mleQRcRc"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=g6MlsYep"><img src="http://feeds.feedburner.com/~f/OraQA?i=g6MlsYep" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/urTi-mDwcLA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2009/01/18/how-to-solve-the-largest-7-digit-number-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2009/01/18/how-to-solve-the-largest-7-digit-number-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the Find Maximum Possible Product Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/C1Vwc3_jneA/</link>
		<comments>http://oraqa.com/2008/12/18/how-to-solve-the-find-maximum-possible-product-puzzle-in-sql/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 05:40:16 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=314</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
Use all the digits from 1 to 9 without repeating, to form two numbers such that their product is maximum.  A digit used should be unique across both the numbers.  For example, the numbers formed could be 1234 and 56789.
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;

SELECT n1, n2, product as [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>Use all the digits from 1 to 9 without repeating, to form two numbers such that their product is maximum.  A digit used should be unique across both the numbers.  For example, the numbers formed could be 1234 and 56789.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<pre>
SELECT n1, n2, product as MAX_PRODUCT
FROM
(SELECT max(n1 * n2 ) over ( ) max_product, n1, n2, n1 * n2 product
 FROM
 (
  WITH DATA AS
  (SELECT replace(sys_connect_by_path(num, ','), ',') str_num
   FROM (SELECT LEVEL num FROM dual CONNECT BY LEVEL &lt;10)
   WHERE LEVEL = 9
   CONNECT BY NOCYCLE PRIOR num != num
   AND LEVEL &lt;= 9
   AND CONNECT_BY_ROOT(num) = 9
  )
  SELECT to_number(substr(str_num,0,LEVEL)) n1, to_number(substr(str_num,LEVEL+1)) n2
  FROM DATA
  CONNECT BY PRIOR str_num = str_num
  AND LEVEL &lt;= length(str_num)-1
  AND PRIOR DBMS_RANDOM.STRING ('P',20) IS NOT NULL
 )
)
WHERE max_product = product;

        N1                  N2                   MAX_PRODUCT
----------               ----------                -----------
      9642                87531                      843973902
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=UW2g3I0B"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=Us4GZl5f"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=6BxqaJcn"><img src="http://feeds.feedburner.com/~f/OraQA?i=6BxqaJcn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=jD4HeDPK"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=ishQ81pz"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=xelJE3XR"><img src="http://feeds.feedburner.com/~f/OraQA?i=xelJE3XR" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/C1Vwc3_jneA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/12/18/how-to-solve-the-find-maximum-possible-product-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/12/18/how-to-solve-the-find-maximum-possible-product-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the Find all 3 digit numbers have the digital sum of nine Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/hOwQTF9iRZo/</link>
		<comments>http://oraqa.com/2008/12/18/how-to-solve-the-find-all-3-digit-numbers-have-the-digital-sum-of-nine-puzzle-in-sql/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 05:37:54 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=309</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
Find out how many 3 digit numbers have the digital sum of nine?
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-

SELECT to_number(str_num) as str_num,
       CASE WHEN ROWNUM = 1
	    THEN count(*) over ( )
       END AS CNT
FROM
(SELECT replace(sys_connect_by_path(num, ','), [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>Find out how many 3 digit numbers have the digital sum of nine?</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre>
SELECT to_number(str_num) as str_num,
       CASE WHEN ROWNUM = 1
	    THEN count(*) over ( )
       END AS CNT
FROM
(SELECT replace(sys_connect_by_path(num, ','), ',') str_num
 FROM (SELECT LEVEL - 1 num FROM dual CONNECT BY LEVEL &lt;=10)
 WHERE LEVEL = 3
 CONNECT BY LEVEL &lt; 4
 AND CASE LEVEL
          WHEN 2
          THEN CASE WHEN (PRIOR num != 0) AND (PRIOR num + num &lt;= 9)
		    THEN 1 END
	  WHEN 3
	  THEN CASE WHEN CONNECT_BY_ROOT(num) + PRIOR num + num = 9
		    THEN 1 END
	  ELSE 1  END = 1
 AND PRIOR dbms_random.string('p', 20) IS NOT NULL
);
</pre>
<p>   STR_NUM              CNT<br />
&#8212;&#8212;&#8212;-               &#8212;&#8212;&#8212;-<br />
       108                  45<br />
       117<br />
       126<br />
       135<br />
       144<br />
       153<br />
       162<br />
       171<br />
       180<br />
       207<br />
       216<br />
       225<br />
       234<br />
       243<br />
       252<br />
       261<br />
       270<br />
       306<br />
       315<br />
       324<br />
       333<br />
       342<br />
       351<br />
       360<br />
       405<br />
       414<br />
       423<br />
       432<br />
       441<br />
       450<br />
       504<br />
       513<br />
       522<br />
       531<br />
       540<br />
       603<br />
       612<br />
       621<br />
       630<br />
       702<br />
       711<br />
       720<br />
       801<br />
       810<br />
       900</p>
<p>45 rows selected.</p>
<p>SQL&gt; </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=sefP3sRH"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=6uXu2ARE"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=x4LUvjt2"><img src="http://feeds.feedburner.com/~f/OraQA?i=x4LUvjt2" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=LtzQdJsh"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=qGoLSljx"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=MRtZQly1"><img src="http://feeds.feedburner.com/~f/OraQA?i=MRtZQly1" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/hOwQTF9iRZo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/12/18/how-to-solve-the-find-all-3-digit-numbers-have-the-digital-sum-of-nine-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/12/18/how-to-solve-the-find-all-3-digit-numbers-have-the-digital-sum-of-nine-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>Is it best to use SQL or PL/SQL?</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/IQ889f6YmcA/</link>
		<comments>http://oraqa.com/2008/11/09/is-it-best-to-use-sql-or-plsql/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 06:11:40 +0000</pubDate>
		<dc:creator>Laurent Schneider</dc:creator>
				<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[plsql against sql]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=302</guid>
		<description><![CDATA[The issue is very often related to context switching. A context switch is when you write plsql code that contains sql statement, or sql that contains plsql functions.

select emp,dbms_random.value from emp;

implies a context switch

begin update emp set sal=0; end;

implies a context switch too.
However, it is sometimes better practice to have a few context switches rather [...]]]></description>
			<content:encoded><![CDATA[<p>The issue is very often related to context switching. A context switch is when you write plsql code that contains sql statement, or sql that contains plsql functions.</p>
<pre>
select emp,dbms_random.value from emp;
</pre>
<p>implies a context switch</p>
<pre>
begin update emp set sal=0; end;
</pre>
<p>implies a context switch too.</p>
<p>However, it is sometimes better practice to have a few context switches rather than a way too much complex SQL query, that nobody will ever be able to maintain but you.</p>
<p>Ok, let&#8217;s try to print the equation abc+def=ghi where abcdefghi are digits from 1 to 9 with no duplicate.</p>
<pre>
SQL&gt; create or replace function f return sys.odcivarchar2list pipelined deterministic is
  2    a7 number;
  3    a8 number;
  4    a9 number;
  5  begin
  6    -- How to solve the 3 Digits Plus 3 Digits Puzzle
  7    -- where a1a2a3 + a4a5a6 = a7a8a9 and each a is an unique digit from 1 to 9
  8    for a1 in 1..9 loop
  9      for a2 in 1..9 loop
 10        if a2 not in (a1) then
 11          for a3 in 1..9 loop
 12            if a3 not in (a1,a2)
 13            then
 14              for a4 in 1..9 loop
 15                if a4 not in (a1,a2,a3) and a1+a4&lt;10
 16                then
 17                  for a5 in 1..9 loop
 18                    if a5 not in (a1,a2,a3,a4) and a1*10+a2+a4*10+a5&lt;100
 19                    then
 20                      for a6 in 1..9 loop
 21                        if a6 not in (a1,a2,a3,a4,a5) and a1*100+a2*10+a3+a4*100+a5*10+a6&lt;1000
 22                        then
 23                          a9:=a3+a6;
 24                          a8:=trunc(a9/10);
 25                          a9:=a9-a8*10;
 26                          if a9 not in (a1,a2,a3,a4,a5,a6,0)
 27                          then
 28                            a8:=a8+a2+a5;
 29                            a7:=trunc(a8/10);
 30                            a8:=a8-a7*10;
 31                            if a8 not in (a1,a2,a3,a4,a5,a6,a9,0)
 32                            then
 33                              a7:=a7+a1+a4;
 34                              if a7 not in (a1,a2,a3,a4,a5,a6,a8,a9,0)
 35                              then
 36                                pipe row(a1||a2||a3||'+'||a4||a5||a6||'='||a7||a8||a9);
 37                              end if;
 38                            end if;
 39                          end if;
 40                        end if;
 41                      end loop;
 42                    end if;
 43                  end loop;
 44                end if;
 45              end loop;
 46            end if;
 47          end loop;
 48        end if;
 49      end loop;
 50    end loop;
 51  end;
 52  /

Function created.

SQL&gt;
SQL&gt; set timi on
SQL&gt; select column_value as Equation_str, decode(rownum,1,count(*) over()) cnt from table(f);
124+659=783                 336
125+739=864
...
784+152=936

336 rows selected.

Elapsed: 00:00:00.96
SQL&gt;
SQL&gt; SELECT num1||' + '||num2||' = '|| num3 as Equation_str,
  2         CASE WHEN lag(cnt) over (order by num1)  cnt
  3                OR lag(cnt) over (order by num1) IS NULL
  4       THEN  cnt END AS Counter
  5  FROM
  6  (SELECT num1, num2, num3, count(*) over ( ) as cnt
  7   FROM
  8   (SELECT substr(num,1,3) num1, substr(num,4,3) num2, substr(num,7,3) num3
  9    FROM
 10    (SELECT replace(sys_connect_by_path(n,','), ',') num
 11     FROM (SELECT LEVEL n FROM dual CONNECT BY LEVEL &lt;=9)
 12     WHERE LEVEL = 9
 13     CONNECT BY NOCYCLE PRIOR n != n
 14     AND LEVEL &lt;=9
 15     AND CASE LEVEL
 16              WHEN 1
 17              THEN CASE WHEN n &lt; 9 THEN 1 END
 18              WHEN 4
 19              THEN CASE WHEN n  + CONNECT_BY_ROOT(n) = 3 THEN 1 END
 23                        ELSE CASE WHEN n &gt;= CONNECT_BY_ROOT(n) + 1 THEN 1 END
 24                   END
 25             ELSE 1 END = 1
 26     )
 27    )
 28   WHERE to_number(num1)     + to_number(num2)       =  to_number(num3)
 29   );
124 + 659 = 783             336
125 + 739 = 864
...
784 + 152 = 936

336 rows selected.

Elapsed: 00:00:22.91
</pre>
<p>hmm, do not you just love <i>select * from table(f);</i> ? Just a sinister scheme to seduce you.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=35P7L2hN"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=MQzHwD3O"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=2SeSvUbF"><img src="http://feeds.feedburner.com/~f/OraQA?i=2SeSvUbF" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=zZ9vMtRA"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=gR3KZoMp"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=i8BOONgM"><img src="http://feeds.feedburner.com/~f/OraQA?i=i8BOONgM" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/IQ889f6YmcA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/11/09/is-it-best-to-use-sql-or-plsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/11/09/is-it-best-to-use-sql-or-plsql/</feedburner:origLink></item>
		<item>
		<title>How to solve the 3 Digits Plus 3 Digits Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/R3X5hhAF3Rg/</link>
		<comments>http://oraqa.com/2008/11/02/how-to-solve-the-3-digits-plus-3-digits-puzzle-in-sql/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:06:32 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=283</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
Use the digits 1 to 9 only once in a sum that must be a three-digit number plus another three-digit number to equal another three-digit number. Each digit can only be used once, but all must be used.
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-
SELECT num1&#124;&#124;' + '&#124;&#124;num2&#124;&#124;' = '&#124;&#124; num3 as Equation_str,
       [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>Use the digits 1 to 9 only once in a sum that must be a three-digit number plus another three-digit number to equal another three-digit number. Each digit can only be used once, but all must be used.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre>SELECT num1||' + '||num2||' = '|| num3 as Equation_str,
       CASE WHEN lag(cnt) over (order by num1) &lt;&gt; cnt
              OR lag(cnt) over (order by num1) IS NULL
     THEN  cnt END AS Counter
FROM
(SELECT num1, num2, num3, count(*) over ( ) as cnt
 FROM
 (SELECT substr(num,1,3) num1, substr(num,4,3) num2, substr(num,7,3) num3
  FROM
  (SELECT replace(sys_connect_by_path(n,','), ',') num
   FROM (SELECT LEVEL n FROM dual CONNECT BY LEVEL &lt;=9)
   WHERE LEVEL = 9
   CONNECT BY NOCYCLE PRIOR n != n
   AND LEVEL &lt;=9
   AND CASE LEVEL
            WHEN 1
            THEN CASE WHEN n &lt; 9 THEN 1 END
            WHEN 4
            THEN CASE WHEN n  + CONNECT_BY_ROOT(n) &lt; 10 THEN 1 END
            WHEN 7
            THEN CASE WHEN CONNECT_BY_ROOT(n) =1
                      THEN CASE WHEN n &gt;= 3 THEN 1 END
                      ELSE CASE WHEN n &gt;= CONNECT_BY_ROOT(n) + 1 THEN 1 END
                 END
           ELSE 1 END = 1
   )
  )
 WHERE to_number(num1)  + to_number(num2)  =  to_number(num3)
 );

EQUATION_STR                                               COUNTER
------------------------------------------                  ----------
124 + 659 = 783                                              336
125 + 739 = 864
127 + 359 = 486
127 + 368 = 495
128 + 367 = 495
128 + 439 = 567
129 + 357 = 486
129 + 735 = 864
129 + 654 = 783
129 + 438 = 567
134 + 658 = 792
.....................................................
.....................................................
.....................................................
.....................................................
.....................................................

762 + 183 = 945
763 + 182 = 945
782 + 154 = 936
782 + 163 = 945
783 + 162 = 945
784 + 152 = 936

336 rows selected.
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=KnL8H1cy"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=9Sx6lKO9"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=VPSb0ZpU"><img src="http://feeds.feedburner.com/~f/OraQA?i=VPSb0ZpU" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=ZEqOtLBY"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=5oJqi8DV"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=bEL2no1m"><img src="http://feeds.feedburner.com/~f/OraQA?i=bEL2no1m" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/R3X5hhAF3Rg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/11/02/how-to-solve-the-3-digits-plus-3-digits-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/11/02/how-to-solve-the-3-digits-plus-3-digits-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the Closest Palindromic Dates Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/JiexeffbfUo/</link>
		<comments>http://oraqa.com/2008/11/02/how-to-solve-the-closest-palindromic-dates-puzzle-in-sql/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:01:48 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=278</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
Using the abbreviation date.month.year (the last two digits of the year), what are the 2 palindromic dates (of any number of digits) closest together in the 1900s?
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-
SELECT date_str, next_date, min_diff_days
FROM
(SELECT min(diff_days) over ( ) min_diff_days, date_str, next_date, diff_days
 FROM
 (SELECT lead(date_str) over (order by pal_date) next_date, [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>Using the abbreviation date.month.year (the last two digits of the year), what are the 2 palindromic dates (of any number of digits) closest together in the 1900s?</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre>SELECT date_str, next_date, min_diff_days
FROM
(SELECT min(diff_days) over ( ) min_diff_days, date_str, next_date, diff_days
 FROM
 (SELECT lead(date_str) over (order by pal_date) next_date, date_str,
         lead(pal_date) over (order by pal_date) - pal_date  as diff_days
  FROM
  (SELECT to_char(to_date('01/01/1900','DD/MM/YYYY')-1+ LEVEL,'fmDD.MM.fmYY') date_str,
          to_date('01/01/1900','DD/MM/YYYY')-1  + LEVEL pal_date
   FROM dual
   CONNECT BY to_date('01/01/1900','MM/DD/YYYY')+LEVEL &lt;= to_date('01/01/2000','MM/DD/YYYY')
  )
  WHERE replace(date_str,'.') =
       utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw((replace(date_str,'.')))))
 )
)
WHERE diff_days = min_diff_days;</pre>
<p>DATE_STR     NEXT_DAT       MIN_DIFF_DAYS<br />
&#8212;&#8212;&#8211;                    &#8212;&#8212;&#8211;        &#8212;&#8212;&#8212;&#8212;-<br />
29.8.92         2.9.92                 4</p>
<p>Elapsed: 00:00:00.41
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=LER3lEjG"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=RNlQmYP5"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=WjI5GlHz"><img src="http://feeds.feedburner.com/~f/OraQA?i=WjI5GlHz" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=DmdYKmkW"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=2hYXSj4t"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=zwtWtcxD"><img src="http://feeds.feedburner.com/~f/OraQA?i=zwtWtcxD" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/JiexeffbfUo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/11/02/how-to-solve-the-closest-palindromic-dates-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/11/02/how-to-solve-the-closest-palindromic-dates-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the 1000 $1 Bills in 10 Envelopes Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/wBp79AprIa0/</link>
		<comments>http://oraqa.com/2008/10/12/how-to-solve-the-1000-1-bills-in-10-envelopes-puzzle-in-sql/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 01:44:30 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=277</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
You are given 1000 one dollar bills and 10 envelopes. Put the bills
into the envelopes in such a way that someone can ask you for any
amount of money from $1 to $1000 (examples $532, $619, $88, etc.)
and you can give it to them through a combination of [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>You are given 1000 one dollar bills and 10 envelopes. Put the bills<br />
into the envelopes in such a way that someone can ask you for any<br />
amount of money from $1 to $1000 (examples $532, $619, $88, etc.)<br />
and you can give it to them through a combination of the envelopes.</p>
<p>The following SQL pattern can be used to solve this puzzle, it also proves<br />
that the result is correct.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<pre>SELECT   ROW_NUMBER () OVER (ORDER BY num) envelope_num,
         num dollars,
         MIN from_min,
         MAX to_max,
         cnt combo_cnt
    FROM (WITH DATA AS
               (SELECT num
                  FROM (SELECT CASE
                                  WHEN SUM (num) OVER (ORDER BY num) > 1000
                                     THEN   num
                                          - (  SUM (num) OVER (ORDER BY num)
                                             - 1000
                                            )
                                  ELSE num
                               END AS num
                          FROM (SELECT     POWER (2, LEVEL - 1) AS num
                                      FROM DUAL
                                CONNECT BY POWER (2, LEVEL - 1) <= 1000))),
               data_check AS
               (SELECT MAX (num) MAX,
                       MIN (num) MIN,
                       COUNT (*) cnt
                  FROM (SELECT DISTINCT SUM
                                           (TO_NUMBER (REGEXP_SUBSTR (str,
                                                                      '[^,]+',
                                                                      1,
                                                                      LEVEL
                                                                     )
                                                      )
                                           ) num
                                   FROM (SELECT     SYS_CONNECT_BY_PATH (num,
                                                                         ','
                                                                        ) str
                                               FROM (SELECT num
                                                       FROM DATA)
                                         CONNECT BY PRIOR num < num)
                             CONNECT BY PRIOR str = str
                                    AND REGEXP_SUBSTR (str, '[^,]+', 1, LEVEL) IS NOT NULL
                                    AND PRIOR DBMS_RANDOM.STRING ('p', 20) IS NOT NULL
                               GROUP BY str))
          SELECT MAX,
                 MIN,
                 cnt,
                 num
            FROM data_check FULL OUTER JOIN DATA ON (data_check.MIN = DATA.num
                                                    )
                 )
ORDER BY num NULLS FIRST;</pre>
<p>SQL&gt; /</p>
<p><code>ENVELOPE_NUM    DOLLARS      FROM_MIN      TO_MAX       COMBO_CNT<br />
------------   ----------   ----------   ----------    ----------<br />
1              1                1         1000          1000<br />
2              2<br />
3              4<br />
4              8<br />
5             16<br />
6             32<br />
7             64<br />
8            128<br />
9            256<br />
10            489</code></p>
<p>10 rows selected.</p>
<p>Elapsed: 00:00:00.43</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=CNbRSdAF"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=Vs5x75jW"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=ta14G767"><img src="http://feeds.feedburner.com/~f/OraQA?i=ta14G767" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=aUk5gDUc"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=TIKhB717"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=hlTGMbkz"><img src="http://feeds.feedburner.com/~f/OraQA?i=hlTGMbkz" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/wBp79AprIa0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/10/12/how-to-solve-the-1000-1-bills-in-10-envelopes-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/10/12/how-to-solve-the-1000-1-bills-in-10-envelopes-puzzle-in-sql/</feedburner:origLink></item>
		<item>
		<title>How to solve the Find the Seven Numbers Puzzle in SQL</title>
		<link>http://feedproxy.google.com/~r/OraQA/~3/oSX2S3L36AA/</link>
		<comments>http://oraqa.com/2008/10/12/how-to-solve-the-find-the-seven-numbers-puzzle-in-sql/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 01:43:08 +0000</pubDate>
		<dc:creator>Frank Zhou</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/?p=275</guid>
		<description><![CDATA[The following is an interesting problem posted by mathforum.org:
There are seven numbers, A, B, C, D, E, F, and G.
When all these numbers except A are added together, 116 is obtained.
When all these numbers except B are added together, 122 is obtained
When all these numbers except C are added together, 123 is obtained
When all these [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an interesting problem posted by <a href="http://mathforum.org/dr.math/">mathforum.org</a>:</p>
<p>There are seven numbers, A, B, C, D, E, F, and G.</p>
<p>When all these numbers except A are added together, 116 is obtained.<br />
When all these numbers except B are added together, 122 is obtained<br />
When all these numbers except C are added together, 123 is obtained<br />
When all these numbers except D are added together, 108 is obtained<br />
When all these numbers except E are added together, 110 is obtained<br />
When all these numbers except F are added together, 119 is obtained<br />
When all these numbers except G are added together, 100 is obtained</p>
<p>Find each of the seven numbers.</p>
<p>COLUMN letter FORMAT A6</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;SQL Solution&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre><code>
SELECT   TRANSLATE (new_str, CHR (0) || str, CHR (0)) letter,
         AVG - num AS num
    FROM (WITH DATA AS
               (SELECT TRANSLATE (old_str, CHR (0) || mis_char, CHR (0))
                                                                       AS str,
                       num
                  FROM (SELECT 'A' AS mis_char,
                               116 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'B' AS mis_char,
                               122 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'C' AS mis_char,
                               123 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'D' AS mis_char,
                               108 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'E' AS mis_char,
                               110 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'F' AS mis_char,
                               119 AS num
                          FROM DUAL
                        UNION ALL
                        SELECT 'G' AS mis_char,
                               100 AS num
                          FROM DUAL),
                       (SELECT 'ABCDEFG' old_str
                          FROM DUAL))
          SELECT str,
                 num,
                 new_str,
                 AVG
            FROM (SELECT str,
                         num
                    FROM DATA),
                 (SELECT XMLAGG (XMLELEMENT (x, str) ORDER BY NULL).EXTRACT
                                                                  ('/X/text()').getstringval
                                                                           ()
                                                                   AS new_str,
                         MAX (AVG) AVG
                    FROM (SELECT   str,
                                   MAX (num) / COUNT (*) AS AVG
                              FROM (SELECT     SUBSTR (str, LEVEL, 1) str,
                                               num
                                          FROM (SELECT XMLAGG
                                                          (XMLELEMENT (x, str) ORDER BY NULL
                                                          ).EXTRACT
                                                                  ('/X/text()').getstringval
                                                                           ()
                                                                          str,
                                                       SUM (num) AS num
                                                  FROM (SELECT str,
                                                               num
                                                          FROM DATA))
                                    CONNECT BY PRIOR str = str
                                           AND LEVEL <= LENGTH (str)
                                           AND PRIOR DBMS_RANDOM.STRING ('P',
                                                                         20
                                                                        ) IS NOT NULL)
                          GROUP BY str)))
ORDER BY letter;
</code></pre>
<pre>LETTER        NUM
------ ----------
A              17
B              11
C              10
D              25
E              23
F              14
G              33</pre>
<p>7 rows selected.</p>
<p>Elapsed: 00:00:00.31</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/OraQA?a=YMn3FBKg"><img src="http://feeds.feedburner.com/~f/OraQA?d=41" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=Dlf3yJfp"><img src="http://feeds.feedburner.com/~f/OraQA?d=50" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=l9ZzDUqM"><img src="http://feeds.feedburner.com/~f/OraQA?i=l9ZzDUqM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=M5XOyMQj"><img src="http://feeds.feedburner.com/~f/OraQA?d=253" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=0EMp23te"><img src="http://feeds.feedburner.com/~f/OraQA?d=45" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/OraQA?a=ZnIBkny2"><img src="http://feeds.feedburner.com/~f/OraQA?i=ZnIBkny2" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/OraQA/~4/oSX2S3L36AA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/10/12/how-to-solve-the-find-the-seven-numbers-puzzle-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://oraqa.com/2008/10/12/how-to-solve-the-find-the-seven-numbers-puzzle-in-sql/</feedburner:origLink></item>
	</channel>
</rss>
