If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > What's wrong here? (NOT IN)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-29-07, 11:23
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
What's wrong here? (NOT IN)

Hello,
seems this query is incorrect. What should do the trick?
Code:
SELECT ID1,ID2,Name
FROM
table1 
WHERE (ID1,ID2) NOT IN
(SELECT ID1,ID2 FROM table2)
Thank you!
Reply With Quote
  #2 (permalink)  
Old 03-29-07, 11:32
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
I think the subquery for the IN clause can only return one column. You probably need to use EXISTS instead.
__________________
Inspiration Through Fermentation
Reply With Quote
  #3 (permalink)  
Old 03-29-07, 11:36
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
This would be correct syntax using the IN clause
Code:
WHERE ID1 NOT IN (SELECT ID1 FROM Table2)
Add an AND/OR statement as needed.
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 03-29-07, 12:02
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
I think AND/OR is not useable here, because it's more than just the combination of ID1 and ID2.
What my query should do: If theres a combination of (ID1,ID2) in table1 but not in table2, I want ID1,ID2,Name from table1.
Reply With Quote
  #5 (permalink)  
Old 03-29-07, 13:07
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Give this a whirl.
It concatenates ID1 and ID2, then compares it to the concatenation of ID1 and ID2 fro Table2.
Not clean/pretty but it might work.
Code:
WHERE ID1 + ID2 NOT IN (SELECT ID1 + ID2 FROM Table2)
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 03-29-07, 13:19
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
Unless ID1 and ID2 are numeric...

7207+1394 = 8601
3233 + 5368 = 8601

A little too much equality there, don't you think?

I still say NOT EXISTS is the way to go.
__________________
Inspiration Through Fermentation
Reply With Quote
  #7 (permalink)  
Old 03-29-07, 19:10
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
ID1 + ' ' + ID2
..?
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 03-29-07, 21:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the answer to the original question ("seems this query is incorrect") is "no, it isn't"

however, despite the fact that it's valid SQL (hint: what forum are we in), very few database systems support row constructors

so perhaps silas could mention which database system the query should run in, and we can stop guessing at the syntax, as george seems to be doing by trying to perform arithmetic on two strings (that'll only work in one of those weird microsoft sql dialects, george)

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 03-30-07, 11:41
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
Sorry, it's SQL Server 2005
Reply With Quote
  #10 (permalink)  
Old 03-30-07, 14:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
this should work in all databases --
Code:
select t1.ID1
     , t1.ID2
     , t1.Name
  from table1 as t1
left outer
  join table2 as t2
    on t2.ID1 = t1.ID1
   and t2.ID2 = t1.ID2
 where t2.ID1 is null
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 03-31-07, 12:10
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Your original query is absolutely fine in standardized SQL. It also works with DB2. So you have a system-specific restriction here.

I wouldn't use the concatenation. First, that usually prevents index usage (concatenation must be done before the comparison). I would write this with a simple NOT EXISTS predicate:
Code:
SELECT ...
FROM   table1 AS t1
WHERE  NOT EXISTS ( SELECT 1
                    FROM table2 AS t2
                    WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id1 )
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #12 (permalink)  
Old 03-31-07, 14:48
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
Thank you for your answers! Seems MS just don't support this. Do the last 2 queries have differences in performance?
Reply With Quote
  #13 (permalink)  
Old 03-31-07, 14:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you could compare the EXPLAINs for starters...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 03-31-07, 14:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oops, microsoft doesn't do EXPLAIN ... try SET SHOWPLAN ON, if i recall correctly
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 03-31-07, 18:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by silas
Do the last 2 queries have differences in performance?
The only way to tell is to actually measure it on your system. So you may want to ask that question in the SQL Server group - not in the group for general SQL questions.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On