<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: &#9733; 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>
	<lastBuildDate>Mon, 21 May 2012 00:26:47 -0700</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<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>&lt;p&gt;select * from t a where a.id in (&amp;1) and not exists (
select * from table(sys.KU$_OBJNUMSET(&amp;1)) b where not exists (
select * from t c where c.id=b.column_value))&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>select * from t a where a.id in (&amp;1) and not exists (
select * from table(sys.KU$_OBJNUMSET(&amp;1)) b where not exists (
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>&lt;p&gt;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&#039;t matter if all the rows get returned.&lt;/p&gt;

&lt;p&gt;Another case of needing to get the exact user requirements...&lt;/p&gt;
</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>&lt;p&gt;If you remove the &quot;id in (select id from u)&quot; (or the equivalent &quot;exists (...)&quot;, won&#039;t the query return values not in the query set? I.E.:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;-- in the first query, 4 is incorrectly returned, yes?&lt;/p&gt;
</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>

<pre><code>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
</code></pre>

<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>&lt;p&gt;Great post!  That is an interesting SQL puzzler!&lt;/p&gt;
</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>&lt;p&gt;There is a problem with Gary&#039;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).&lt;/p&gt;

&lt;p&gt;Jeff&#039;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&#039;s solution could be further improved by removing the where id in clause completely. The Set minus operation does the job neatly.&lt;/p&gt;

&lt;p&gt;So using the improvement on Jeff&#039;s solution and using any method to manipulate the input list, the solution could be as below :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;select * from t
where not exists (
    select to_number(column_name) 
    from Input_Table 
    minus 
    select id from t)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;&lt;code&gt;Input_Table&lt;/code&gt;&lt;/em&gt; can be computed from input list of values either using PL/SQL method (Partha), &lt;code&gt;sys.dbms_debug_vc2coll&lt;/code&gt; (Gary) or temporary table method (Jeff). This would be driven by the assumptions you make of the input data set.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;&lt;code&gt;column_name&lt;/code&gt;&lt;/em&gt; would be &lt;code&gt;column_name&lt;/code&gt; in case of PL/SQL method, &lt;code&gt;column_value&lt;/code&gt; in case of &lt;code&gt;dbms_debug_vc2coll&lt;/code&gt; method or the column name of the temporary table in case of temporary table method.&lt;/p&gt;
</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>

<pre><code>select * from t
where not exists (
    select to_number(column_name) 
    from Input_Table 
    minus 
    select id from t)
</code></pre>

<p>The <em><code>Input_Table</code></em> can be computed from input list of values either using PL/SQL method (Partha), <code>sys.dbms_debug_vc2coll</code> (Gary) or temporary table method (Jeff). This would be driven by the assumptions you make of the input data set.</p>

<p>The <em><code>column_name</code></em> would be <code>column_name</code> in case of PL/SQL method, <code>column_value</code> in case of <code>dbms_debug_vc2coll</code> 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>&lt;p&gt;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&#039;s solution).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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
/
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>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&#8217;s solution).</p>

<pre><code>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
/
</code></pre>
]]></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>&lt;p&gt;Perhaps simplistic, but here&#039;s my take on this problem:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://jeffkemponoracle.blogspot.com/2005/10/sql-problem.html&quot; rel=&quot;nofollow&quot;&gt;jeffkemponoracle.blogspot.com/2005/10/sql-problem.html&lt;/a&gt;&lt;/p&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>&lt;p&gt;OK, another (shorter) try at posting this (id_domain contains 1..1000):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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
        )
);
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>OK, another (shorter) try at posting this (id_domain contains 1..1000):</p>

<pre><code>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
        )
);
</code></pre>
]]></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>&lt;p&gt;How about these two methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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 
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>How about these two methods:</p>

<pre><code>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 
</code></pre>
]]></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>&lt;p&gt;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 &lt;code&gt;to_number&lt;/code&gt;, a &lt;code&gt;to_char&lt;/code&gt; needs to be added as well. that way, it&#039;ll work for both numeric and alphanumeric all the time:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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(&#039;1,a,2&#039;) 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(&#039;1,a,2&#039;) as tablist))
  6      union select to_char(id) from t)) a,
  7                (select count(*) cnt_b from t) b
  8       )
  9  /

no rows selected
&lt;/code&gt;&lt;/pre&gt;
</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 <code>to_number</code>, a <code>to_char</code> needs to be added as well. that way, it&#8217;ll work for both numeric and alphanumeric all the time:</p>

<pre><code>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
</code></pre>
]]></content:encoded>
	</item>
</channel>
</rss>

