<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Eddie Awad’s Blog - Latest Comments in SQL problem</title><link>http://awads.disqus.com/</link><description>News, views, tips and tricks on Oracle and other fun stuff</description><atom:link href="https://awads.disqus.com/sql_problem/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 11 Jun 2008 01:03:59 -0000</lastBuildDate><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657931</link><description>&lt;p&gt;select * from t a where &lt;a href="http://a.id" rel="nofollow noopener" target="_blank" title="a.id"&gt;a.id&lt;/a&gt; in (&amp;amp;1) and not exists (&lt;br&gt;select * from table(sys.KU$_OBJNUMSET(&amp;amp;1)) b where not exists (&lt;br&gt;select * from t c where &lt;a href="http://c.id" rel="nofollow noopener" target="_blank" title="c.id"&gt;c.id&lt;/a&gt;=b.column_value))&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">evan</dc:creator><pubDate>Wed, 11 Jun 2008 01:03:59 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657930</link><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'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><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jeffrey Kemp</dc:creator><pubDate>Thu, 13 Oct 2005 01:16:21 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657929</link><description>&lt;p&gt;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.:&lt;/p&gt;&lt;p&gt;    SQL&amp;gt; select * from t;&lt;br&gt;            ID&lt;br&gt;    ----------&lt;br&gt;             1&lt;br&gt;             2&lt;br&gt;             3&lt;/p&gt;&lt;p&gt;    SQL&amp;gt; select * from u;&lt;br&gt;            ID&lt;br&gt;    ----------&lt;br&gt;             1&lt;br&gt;             2&lt;br&gt;             3&lt;/p&gt;&lt;p&gt;    SQL&amp;gt; insert into t values (4);&lt;br&gt;    1 row created.&lt;/p&gt;&lt;p&gt;    SQL&amp;gt; select * from t &lt;br&gt;        where not exists (select id from u minus select id from t);&lt;br&gt;            ID&lt;br&gt;    ----------&lt;br&gt;             1&lt;br&gt;             2&lt;br&gt;             4&lt;br&gt;             3&lt;/p&gt;&lt;p&gt;    SQL&amp;gt; select * from t where id in (select id from u) &lt;br&gt;        and not exists (select id from u minus select id from t);&lt;br&gt;            ID&lt;br&gt;    ----------&lt;br&gt;             1&lt;br&gt;             2&lt;br&gt;             3&lt;/p&gt;&lt;p&gt;-- in the first query, 4 is incorrectly returned, yes?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jeffrey Kemp</dc:creator><pubDate>Thu, 13 Oct 2005 01:10:30 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657928</link><description>&lt;p&gt;Great post!  That is an interesting SQL puzzler!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Robert Vollman</dc:creator><pubDate>Wed, 12 Oct 2005 13:32:43 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657927</link><description>&lt;p&gt;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).&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;So using the improvement on Jeff's solution and using any method to manipulate the input list, the solution could be as below :&lt;/p&gt;&lt;p&gt;    select * from t&lt;br&gt;    where not exists (&lt;br&gt;        select to_number(column_name) &lt;br&gt;        from Input_Table &lt;br&gt;        minus &lt;br&gt;        select id from t)&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Partha</dc:creator><pubDate>Wed, 12 Oct 2005 07:12:04 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657924</link><description>&lt;p&gt;It uses a collection type (but in my case I use the predefined one).&lt;br&gt;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).&lt;/p&gt;&lt;p&gt;    create table t(id number);&lt;/p&gt;&lt;p&gt;    insert into t values(1);&lt;/p&gt;&lt;p&gt;    insert into t values(2);&lt;/p&gt;&lt;p&gt;    select * from &lt;br&gt;    (select &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;, x.column_value x, &lt;br&gt;    count(distinct x.column_value) over () count_x,&lt;br&gt;    count(distinct &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;) over () count_t&lt;br&gt;    from t, table(sys.dbms_debug_vc2coll(1,2,3)) x&lt;br&gt;    where &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;(+) = to_number(x.column_value))&lt;br&gt;    where count_x = count_t&lt;br&gt;    /&lt;/p&gt;&lt;p&gt;    select * from &lt;br&gt;    (select &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;, x.column_value x, &lt;br&gt;    count(distinct x.column_value) over () count_x,&lt;br&gt;    count(distinct &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;) over () count_t&lt;br&gt;    from t, table(sys.dbms_debug_vc2coll(1,2)) x&lt;br&gt;    where &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;(+) = to_number(x.column_value))&lt;br&gt;    where count_x = count_t&lt;br&gt;    /&lt;/p&gt;&lt;p&gt;    select * from &lt;br&gt;    (select &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;, x.column_value x, &lt;br&gt;    count(distinct x.column_value) over () count_x,&lt;br&gt;    count(distinct &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;) over () count_t&lt;br&gt;    from t, table(sys.dbms_debug_vc2coll(1)) x&lt;br&gt;    where &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;(+) = to_number(x.column_value))&lt;br&gt;    where count_x = count_t&lt;br&gt;    /&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Gary</dc:creator><pubDate>Wed, 12 Oct 2005 02:35:18 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657926</link><description>&lt;p&gt;Perhaps simplistic, but here's my take on this problem:&lt;/p&gt;&lt;p&gt;&lt;a href="http://jeffkemponoracle.blogspot.com/2005/10/sql-problem.html" rel="nofollow noopener" target="_blank" title="http://jeffkemponoracle.blogspot.com/2005/10/sql-problem.html"&gt;jeffkemponoracle.blogspot.com/2005/10/sql-problem.html&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jeffrey Kemp</dc:creator><pubDate>Wed, 12 Oct 2005 01:33:59 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657925</link><description>&lt;p&gt;OK, another (shorter) try at posting this (id_domain contains 1..1000):&lt;/p&gt;&lt;p&gt;    select id&lt;br&gt;    from (&lt;br&gt;    	select &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;, count(distinct &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt;) over () cnt1, cnt2&lt;br&gt;    	from (&lt;br&gt;    		select id, count(id) over () cnt2&lt;br&gt;    		from id_domain&lt;br&gt;    		where id in (1,2,17,17)&lt;br&gt;    	) p, id_tab t&lt;br&gt;    	where &lt;a href="http://t.id" rel="nofollow noopener" target="_blank" title="t.id"&gt;t.id&lt;/a&gt; = &lt;a href="http://p.id" rel="nofollow noopener" target="_blank" title="p.id"&gt;p.id&lt;/a&gt;&lt;br&gt;    )&lt;br&gt;    where cnt1 = cnt2;&lt;/p&gt;&lt;p&gt;    select id&lt;br&gt;    from (&lt;br&gt;    	select id&lt;br&gt;    	from id_tab&lt;br&gt;    	where id in (1,2,17,17)&lt;br&gt;    )&lt;br&gt;    where not exists (&lt;br&gt;    	select null&lt;br&gt;    	from id_domain&lt;br&gt;    	where id in (1,2,17,17)&lt;br&gt;    		and id not in (&lt;br&gt;    			select id&lt;br&gt;    			from id_tab&lt;br&gt;    		)&lt;br&gt;    );&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mr. Ed</dc:creator><pubDate>Wed, 12 Oct 2005 00:52:25 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657923</link><description>&lt;p&gt;How about these two methods:&lt;/p&gt;&lt;p&gt;    create table id_tab (id number);&lt;br&gt;    insert into id_tab select rn &lt;br&gt;        from (select rownum rn from dba_objects) where mod(rn, 20) != 0;&lt;br&gt;    insert into id_tab select rn &lt;br&gt;        from (select rownum rn from dba_objects) where mod(rn, 10) != 0;&lt;br&gt;    insert into id_tab select rn &lt;br&gt;        from (select rownum rn from dba_objects) where mod(rn, 5) != 0;&lt;br&gt;    commit;&lt;/p&gt;&lt;p&gt;    create table id_domain (id number);&lt;br&gt;    insert into id_domain select rownum from dba_objects where rownum&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mr. Ed</dc:creator><pubDate>Wed, 12 Oct 2005 00:46:14 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657922</link><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 `to_number`, a `to_char` needs to be added as well. that way, it'll work for both numeric and alphanumeric all the time:&lt;/p&gt;&lt;p&gt;    scott: select * from t&lt;br&gt;      2  where 0=(select cnt_a-cnt_b&lt;br&gt;      3      from (select count(*) cnt_a&lt;br&gt;      4      from (select column_value&lt;br&gt;      5      from table(cast (my_comma_to_table('1,a,2') as tablist))&lt;br&gt;      6      union select id from t)) a,&lt;br&gt;      7                (select count(*) cnt_b from t) b&lt;br&gt;      8       )&lt;br&gt;      9  /&lt;br&gt;        from (select column_value&lt;br&gt;                     *&lt;br&gt;    ERROR at line 4:&lt;br&gt;    ORA-01790: expression must have same datatype as &lt;br&gt;    corresponding expression&lt;/p&gt;&lt;p&gt;    scott: select * from t&lt;br&gt;      2  where 0=(select cnt_a-cnt_b&lt;br&gt;      3      from (select count(*) cnt_a&lt;br&gt;      4      from (select column_value&lt;br&gt;      5      from table(cast (my_comma_to_table('1,a,2') as tablist))&lt;br&gt;      6      union select to_char(id) from t)) a,&lt;br&gt;      7                (select count(*) cnt_b from t) b&lt;br&gt;      8       )&lt;br&gt;      9  /&lt;/p&gt;&lt;p&gt;    no rows selected&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Tue, 11 Oct 2005 13:23:04 -0000</pubDate></item><item><title>Re: SQL problem</title><link>http://awads.net/wp/2005/10/11/sql-problem/#comment-3657921</link><description>&lt;p&gt;There is a more generic way, but it would involve creating a SQL type and a function to convert comma-seperated list to a collection. Then using set logic you can satisfy the requirement and it will handle duplicate values as well in the input. The solution given below has been tested using the table structure as you have shown above, but it can handle any type of table:&lt;/p&gt;&lt;p&gt;    create or replace type tablist as table of varchar2(255);&lt;/p&gt;&lt;p&gt;    create or replace function my_comma_to_table(csv varchar2)&lt;br&gt;    return tablist is&lt;br&gt;      l_string        long default csv || ',';&lt;br&gt;      l_data          tablist := tablist();&lt;br&gt;      n               number;&lt;br&gt;    begin&lt;br&gt;    loop&lt;br&gt;      exit when l_string is null;&lt;br&gt;      n := instr( l_string, ',' );&lt;br&gt;      l_data.extend;&lt;br&gt;      l_data(l_data.count) := &lt;br&gt;        ltrim( rtrim( substr( l_string, 1, n-1 ) ) );&lt;br&gt;      l_string := substr( l_string, n+1 );&lt;br&gt;    end loop;&lt;br&gt;    return l_data;&lt;br&gt;    end my_comma_to_table;&lt;/p&gt;&lt;p&gt;    select * from t&lt;br&gt;    where 0=(select cnt_a-cnt_b&lt;br&gt;        from (select count(*) cnt_a &lt;br&gt;        from (select to_number(column_value) &lt;br&gt;        from table(cast (my_comma_to_table('1,2,2') as tablist)) &lt;br&gt;        union select id from t)) a,&lt;br&gt;                  (select count(*) cnt_b from t) b&lt;br&gt;         )&lt;br&gt;    /&lt;/p&gt;&lt;p&gt;The above would handle even when the list contains alphanumeric values. In that case just remove the to\_number function in the select query.&lt;/p&gt;&lt;p&gt;Regards &lt;br&gt;Partha&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Partha</dc:creator><pubDate>Tue, 11 Oct 2005 11:59:29 -0000</pubDate></item></channel></rss>