<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	>
<channel>
	<title>Comments on: SQL problem</title>
	<atom:link href="http://awads.net/wp/2005/10/11/sql-problem/feed/" rel="self" type="application/rss+xml" />
	<link>http://awads.net/wp/2005/10/11/sql-problem/</link>
	<description>News, views, tips and tricks on Oracle and other fun stuff</description>
	<pubDate>Tue, 06 Jan 2009 05:12:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: evan</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-52146</link>
		<dc:creator>evan</dc:creator>
		<pubDate>Wed, 11 Jun 2008 05:03:59 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-52146</guid>
		<description>select * from t a where a.id in (&#38;1) and not exists (
select * from table(sys.KU$_OBJNUMSET(&#38;1)) b where not exists (
select * from t c where c.id=b.column_value))</description>
		<content:encoded><![CDATA[<p>select * from t a where a.id in (&amp;1) and not exists (<br />
select * from table(sys.KU$_OBJNUMSET(&amp;1)) b where not exists (<br />
select * from t c where c.id=b.column_value))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Kemp</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-215</link>
		<dc:creator>Jeffrey Kemp</dc:creator>
		<pubDate>Thu, 13 Oct 2005 05:16:21 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-215</guid>
		<description>After re-reading the original post it appears it was not a requirement to limit the rows returned unless all the query rows existed in it; so my last comment is probably superfluous if it doesn't matter if all the rows get returned.

Another case of needing to get the exact user requirements...</description>
		<content:encoded><![CDATA[<p>After re-reading the original post it appears it was not a requirement to limit the rows returned unless all the query rows existed in it; so my last comment is probably superfluous if it doesn&#8217;t matter if all the rows get returned.</p>
<p>Another case of needing to get the exact user requirements&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Kemp</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-214</link>
		<dc:creator>Jeffrey Kemp</dc:creator>
		<pubDate>Thu, 13 Oct 2005 05:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-214</guid>
		<description>If you remove the "id in (select id from u)" (or the equivalent "exists (...)", won't the query return values not in the query set? I.E.:

    SQL&gt; select * from t;
            ID
    ----------
             1
             2
             3

    SQL&gt; select * from u;
            ID
    ----------
             1
             2
             3

    SQL&gt; insert into t values (4);
    1 row created.

    SQL&gt; select * from t 
        where not exists (select id from u minus select id from t);
            ID
    ----------
             1
             2
             4
             3

    SQL&gt; select * from t where id in (select id from u) 
        and not exists (select id from u minus select id from t);
            ID
    ----------
             1
             2
             3

-- in the first query, 4 is incorrectly returned, yes?</description>
		<content:encoded><![CDATA[<p>If you remove the &#8220;id in (select id from u)&#8221; (or the equivalent &#8220;exists (&#8230;)&#8221;, won&#8217;t the query return values not in the query set? I.E.:</p>
<p>    SQL> select * from t;<br />
            ID<br />
    &#8212;&#8212;&#8212;-<br />
             1<br />
             2<br />
             3</p>
<p>    SQL> select * from u;<br />
            ID<br />
    &#8212;&#8212;&#8212;-<br />
             1<br />
             2<br />
             3</p>
<p>    SQL> insert into t values (4);<br />
    1 row created.</p>
<p>    SQL> select * from t<br />
        where not exists (select id from u minus select id from t);<br />
            ID<br />
    &#8212;&#8212;&#8212;-<br />
             1<br />
             2<br />
             4<br />
             3</p>
<p>    SQL> select * from t where id in (select id from u)<br />
        and not exists (select id from u minus select id from t);<br />
            ID<br />
    &#8212;&#8212;&#8212;-<br />
             1<br />
             2<br />
             3</p>
<p>&#8211; in the first query, 4 is incorrectly returned, yes?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Vollman</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-211</link>
		<dc:creator>Robert Vollman</dc:creator>
		<pubDate>Wed, 12 Oct 2005 17:32:43 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-211</guid>
		<description>Great post!  That is an interesting SQL puzzler!</description>
		<content:encoded><![CDATA[<p>Great post!  That is an interesting SQL puzzler!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Partha</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-208</link>
		<dc:creator>Partha</dc:creator>
		<pubDate>Wed, 12 Oct 2005 11:12:04 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-208</guid>
		<description>There is a problem with Gary's solution as it would give duplicate values if the collection contained any duplications. As per the requirement, we cannot have duplicate rows returned from t even if the values appear duplicate in the input list or collection(whatever you might call it).  

Jeff's solution was what I wanted to acheive, but really could not get it working. I got around to it using Set Union, but not a very neat solution. Please note the trick for removing duplicate values was using a Set operation. Jeff's solution could be further improved by removing the where id in clause completely. The Set minus operation does the job neatly.   

So using the improvement on Jeff's solution and using any method to manipulate the input list, the solution could be as below :   

    select * from t
    where not exists (
        select to_number(column_name) 
        from Input_Table 
        minus 
        select id from t)

The *`Input_Table`* can be computed from input list of values either using PL/SQL method (Partha), `sys.dbms_debug_vc2coll` (Gary) or temporary table method (Jeff). This would be driven by the assumptions you make of the input data set.

The *`column_name`* would be `column_name` in case of PL/SQL method, `column_value` in case of `dbms_debug_vc2coll` method or the column name of the temporary table in case of temporary table method.</description>
		<content:encoded><![CDATA[<p>There is a problem with Gary&#8217;s solution as it would give duplicate values if the collection contained any duplications. As per the requirement, we cannot have duplicate rows returned from t even if the values appear duplicate in the input list or collection(whatever you might call it).  </p>
<p>Jeff&#8217;s solution was what I wanted to acheive, but really could not get it working. I got around to it using Set Union, but not a very neat solution. Please note the trick for removing duplicate values was using a Set operation. Jeff&#8217;s solution could be further improved by removing the where id in clause completely. The Set minus operation does the job neatly.   </p>
<p>So using the improvement on Jeff&#8217;s solution and using any method to manipulate the input list, the solution could be as below :   </p>
<p>    select * from t<br />
    where not exists (<br />
        select to_number(column_name)<br />
        from Input_Table<br />
        minus<br />
        select id from t)</p>
<p>The *`Input_Table`* can be computed from input list of values either using PL/SQL method (Partha), `sys.dbms_debug_vc2coll` (Gary) or temporary table method (Jeff). This would be driven by the assumptions you make of the input data set.</p>
<p>The *`column_name`* would be `column_name` in case of PL/SQL method, `column_value` in case of `dbms_debug_vc2coll` method or the column name of the temporary table in case of temporary table method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-206</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Wed, 12 Oct 2005 06:35:18 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-206</guid>
		<description>It uses a collection type (but in my case I use the predefined one).
Depends on whether they want to pass the list as a single variable (which will need parsing into its components using PL/SQL as per Partha's solution).


    create table t(id number);

    insert into t values(1);

    insert into t values(2);

    select * from 
    (select t.id, x.column_value x, 
    count(distinct x.column_value) over () count_x,
    count(distinct t.id) over () count_t
    from t, table(sys.dbms_debug_vc2coll(1,2,3)) x
    where t.id(+) = to_number(x.column_value))
    where count_x = count_t
    /

    select * from 
    (select t.id, x.column_value x, 
    count(distinct x.column_value) over () count_x,
    count(distinct t.id) over () count_t
    from t, table(sys.dbms_debug_vc2coll(1,2)) x
    where t.id(+) = to_number(x.column_value))
    where count_x = count_t
    /

    select * from 
    (select t.id, x.column_value x, 
    count(distinct x.column_value) over () count_x,
    count(distinct t.id) over () count_t
    from t, table(sys.dbms_debug_vc2coll(1)) x
    where t.id(+) = to_number(x.column_value))
    where count_x = count_t
    /
</description>
		<content:encoded><![CDATA[<p>It uses a collection type (but in my case I use the predefined one).<br />
Depends on whether they want to pass the list as a single variable (which will need parsing into its components using PL/SQL as per Partha&#8217;s solution).</p>
<p>    create table t(id number);</p>
<p>    insert into t values(1);</p>
<p>    insert into t values(2);</p>
<p>    select * from<br />
    (select t.id, x.column_value x,<br />
    count(distinct x.column_value) over () count_x,<br />
    count(distinct t.id) over () count_t<br />
    from t, table(sys.dbms_debug_vc2coll(1,2,3)) x<br />
    where t.id(+) = to_number(x.column_value))<br />
    where count_x = count_t<br />
    /</p>
<p>    select * from<br />
    (select t.id, x.column_value x,<br />
    count(distinct x.column_value) over () count_x,<br />
    count(distinct t.id) over () count_t<br />
    from t, table(sys.dbms_debug_vc2coll(1,2)) x<br />
    where t.id(+) = to_number(x.column_value))<br />
    where count_x = count_t<br />
    /</p>
<p>    select * from<br />
    (select t.id, x.column_value x,<br />
    count(distinct x.column_value) over () count_x,<br />
    count(distinct t.id) over () count_t<br />
    from t, table(sys.dbms_debug_vc2coll(1)) x<br />
    where t.id(+) = to_number(x.column_value))<br />
    where count_x = count_t<br />
    /</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Kemp</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-203</link>
		<dc:creator>Jeffrey Kemp</dc:creator>
		<pubDate>Wed, 12 Oct 2005 05:33:59 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-203</guid>
		<description>Perhaps simplistic, but here's my take on this problem:

&lt;a href="http://jeffkemponoracle.blogspot.com/2005/10/sql-problem.html" rel="nofollow"&gt;jeffkemponoracle.blogspot.com/2005/10/sql-problem.html&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Perhaps simplistic, but here&#8217;s my take on this problem:</p>
<p><a href="http://jeffkemponoracle.blogspot.com/2005/10/sql-problem.html" rel="nofollow">jeffkemponoracle.blogspot.com/2005/10/sql-problem.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mr. Ed</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-202</link>
		<dc:creator>Mr. Ed</dc:creator>
		<pubDate>Wed, 12 Oct 2005 04:52:25 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-202</guid>
		<description>OK, another (shorter) try at posting this (id_domain contains 1..1000):


    select id
    from (
    	select t.id, count(distinct t.id) over () cnt1, cnt2
    	from (
    		select id, count(id) over () cnt2
    		from id_domain
    		where id in (1,2,17,17)
    	) p, id_tab t
    	where t.id = p.id
    )
    where cnt1 = cnt2;


    select id
    from (
    	select id
    	from id_tab
    	where id in (1,2,17,17)
    )
    where not exists (
    	select null
    	from id_domain
    	where id in (1,2,17,17)
    		and id not in (
    			select id
    			from id_tab
    		)
    );
</description>
		<content:encoded><![CDATA[<p>OK, another (shorter) try at posting this (id_domain contains 1..1000):</p>
<p>    select id<br />
    from (<br />
    	select t.id, count(distinct t.id) over () cnt1, cnt2<br />
    	from (<br />
    		select id, count(id) over () cnt2<br />
    		from id_domain<br />
    		where id in (1,2,17,17)<br />
    	) p, id_tab t<br />
    	where t.id = p.id<br />
    )<br />
    where cnt1 = cnt2;</p>
<p>    select id<br />
    from (<br />
    	select id<br />
    	from id_tab<br />
    	where id in (1,2,17,17)<br />
    )<br />
    where not exists (<br />
    	select null<br />
    	from id_domain<br />
    	where id in (1,2,17,17)<br />
    		and id not in (<br />
    			select id<br />
    			from id_tab<br />
    		)<br />
    );</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mr. Ed</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-200</link>
		<dc:creator>Mr. Ed</dc:creator>
		<pubDate>Wed, 12 Oct 2005 04:46:14 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-200</guid>
		<description>How about these two methods:


    create table id_tab (id number);
    insert into id_tab select rn 
        from (select rownum rn from dba_objects) where mod(rn, 20) != 0;
    insert into id_tab select rn 
        from (select rownum rn from dba_objects) where mod(rn, 10) != 0;
    insert into id_tab select rn 
        from (select rownum rn from dba_objects) where mod(rn, 5) != 0;
    commit;

    create table id_domain (id number);
    insert into id_domain select rownum from dba_objects where rownum </description>
		<content:encoded><![CDATA[<p>How about these two methods:</p>
<p>    create table id_tab (id number);<br />
    insert into id_tab select rn<br />
        from (select rownum rn from dba_objects) where mod(rn, 20) != 0;<br />
    insert into id_tab select rn<br />
        from (select rownum rn from dba_objects) where mod(rn, 10) != 0;<br />
    insert into id_tab select rn<br />
        from (select rownum rn from dba_objects) where mod(rn, 5) != 0;<br />
    commit;</p>
<p>    create table id_domain (id number);<br />
    insert into id_domain select rownum from dba_objects where rownum</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eddie Awad</title>
		<link>http://awads.net/wp/2005/10/11/sql-problem/comment-page-1/#comment-197</link>
		<dc:creator>Eddie Awad</dc:creator>
		<pubDate>Tue, 11 Oct 2005 17:23:04 +0000</pubDate>
		<guid isPermaLink="false">http://awads.net/wp/?p=139#comment-197</guid>
		<description>Partha, thanks, your solution works great (even though it needed some PL/SQL help). However, a minor modification to the query is needed to make it work for both numeric and alphanumeric input. In addition to removing the `to_number`, a `to_char` needs to be added as well. that way, it'll work for both numeric and alphanumeric all the time:

    scott: select * from t
      2  where 0=(select cnt_a-cnt_b
      3      from (select count(*) cnt_a
      4      from (select column_value
      5      from table(cast (my_comma_to_table('1,a,2') as tablist))
      6      union select id from t)) a,
      7                (select count(*) cnt_b from t) b
      8       )
      9  /
        from (select column_value
                     *
    ERROR at line 4:
    ORA-01790: expression must have same datatype as 
    corresponding expression
    
    
    scott: select * from t
      2  where 0=(select cnt_a-cnt_b
      3      from (select count(*) cnt_a
      4      from (select column_value
      5      from table(cast (my_comma_to_table('1,a,2') as tablist))
      6      union select to_char(id) from t)) a,
      7                (select count(*) cnt_b from t) b
      8       )
      9  /
    
    no rows selected</description>
		<content:encoded><![CDATA[<p>Partha, thanks, your solution works great (even though it needed some PL/SQL help). However, a minor modification to the query is needed to make it work for both numeric and alphanumeric input. In addition to removing the `to_number`, a `to_char` needs to be added as well. that way, it&#8217;ll work for both numeric and alphanumeric all the time:</p>
<p>    scott: select * from t<br />
      2  where 0=(select cnt_a-cnt_b<br />
      3      from (select count(*) cnt_a<br />
      4      from (select column_value<br />
      5      from table(cast (my_comma_to_table(&#8217;1,a,2&#8242;) as tablist))<br />
      6      union select id from t)) a,<br />
      7                (select count(*) cnt_b from t) b<br />
      8       )<br />
      9  /<br />
        from (select column_value<br />
                     *<br />
    ERROR at line 4:<br />
    ORA-01790: expression must have same datatype as<br />
    corresponding expression</p>
<p>    scott: select * from t<br />
      2  where 0=(select cnt_a-cnt_b<br />
      3      from (select count(*) cnt_a<br />
      4      from (select column_value<br />
      5      from table(cast (my_comma_to_table(&#8217;1,a,2&#8242;) as tablist))<br />
      6      union select to_char(id) from t)) a,<br />
      7                (select count(*) cnt_b from t) b<br />
      8       )<br />
      9  /</p>
<p>    no rows selected</p>
]]></content:encoded>
	</item>
</channel>
</rss>
