| |
|
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.
|
 |

05-01-09, 23:49
|
|
Registered User
|
|
Join Date: Jul 2006
Posts: 3
|
|
|
Need help formulating a non-equal join
|
|
I'm trying to get all records in table ADRS whose zip codes are NOT present in table EXCLUDE's zip code column.
Th following gives me the exact inverse of what I want (e.g. all records in ADRS whose zip codes DO match some zip code in EXCLUDE:
SELECT ADRS.First, ADRS.Last, ADRS.Adr, ADRS.City, ADRS.St, ADRS.Zip
FROM ADRS INNER JOIN EXCLUDE ON ADRS.Zip =EXCLUDE.Zip;
BTW, ADRS.Zip is never null.
How can I turn this query "upside down"?
Any help greatly appreciated
Tnx,
Lenny Wintfeld
|
|

05-02-09, 06:04
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
|
|
Code:
SELECT ADRS.First
, ADRS.Last
, ADRS.Adr
, ADRS.City
, ADRS.St
, ADRS.Zip
FROM ADRS
LEFT OUTER
JOIN EXCLUDE
ON EXCLUDE.Zip = ADRS.Zip
WHERE EXCLUDE.Zip IS NULL

|
|

05-02-09, 10:37
|
|
Registered User
|
|
Join Date: Jul 2006
Posts: 3
|
|
|
Thanks for your reply
|
|
It worked out perfectly.
I do great with realtime and multithreaded applications but for some reason non-procedural languages like sql and xslt are a blind spot. I really should buy your book
Many thanks. --Lenny
|
|

05-17-09, 15:57
|
|
Registered User
|
|
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
|
|
Quote:
|
Originally Posted by lennyw
I'm trying to get all records in table ADRS whose zip codes are NOT present in table EXCLUDE's zip code column.
|
Alternative is to use NOT EXISTS and a subquery:
Code:
SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE NOT EXISTS (SELECT 1
FROM EXCLUDE
WHERE Zip = ADRS.Zip)
This should normally be equally performant as the outer join.
I often perfer it over the join solution because each of the two queries refers to a single table (hence does not need the table prefix for the column names).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
|
Last edited by Peter.Vanroose; 05-17-09 at 16:07.
|

07-01-09, 10:45
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 17
|
|
SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE Zip NOT IN (SELECT EXCLUDE.zip
FROM EXCLUDE
)
|
|

07-02-09, 18:05
|
|
Registered User
|
|
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
|
|
Quote:
|
Originally Posted by Vaibhav Vyas
SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE Zip NOT IN (SELECT EXCLUDE.zip
FROM EXCLUDE
)
|
Beware that this will return an empty result whenever there is a row in EXCLUDE for which ZIP is NULL.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
|
|

07-02-09, 18:09
|
|
Registered User
|
|
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
|
|
Or yet an other way to write it:
Code:
SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE Zip IN (SELECT Zip FROM adrs
EXCEPT
SELECT Zip FROM EXCLUDE)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|