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 > Database Server Software > DB2 > Multi column subqueries in DB2?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-08, 12:15
nottoknow nottoknow is offline
Registered User
 
Join Date: Nov 2008
Posts: 1
Is there a way to write multi-column subqueries in DB2? I tried following

select * from table1 where (field1,field2) not in (select field1,field2 from table2 )

thanks
Reply With Quote
  #2 (permalink)  
Old 11-27-08, 12:39
arlf arlf is offline
Registered User
 
Join Date: May 2006
Posts: 16
Try

select * from table1 a
where not exists ( Select 1 from table2 b
where b.field1 = a.field1
and b.field2 = a.field2 )

Saludos, ARLF.
Reply With Quote
  #3 (permalink)  
Old 11-27-08, 12:59
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Both queries are perfectly fine. What's the problem you have with the first, non-correlated subquery?

Code:
$ db2 "select * from t where (a, b) not in ( select c1, c2 from x )"

A           B
----------- -----------

  0 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 11-27-08, 13:14
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
select * from table1 where (field1,field2) not in (select field1,field2 from table2 )
You can do that in DB2 for LUW. DB2 for z/OS also support that syntax.

For more general syntax, you can see in
"DB2 SQL Reference" ---> "Chapter 2. Language elements" ---> "Predicates" ---> "IN predicate" and "Quantified predicate".

You can see DB2 9 for LUW PDF manuals here.
http://www-01.ibm.com/support/docvie...id=swg27009552
or
http://www-01.ibm.com/support/docvie...id=swg27009552
DB2 9 for Linux, UNIX and Windows manuals

Manual

Abstract
This page contains English Portable Document Format (PDF) manuals for DB2® 9 for Linux®, UNIX® and Windows® products, and additional components such as DB2 Connect™ and WebSphere® Federation Server.
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