Groups | Blog | Home
all groups > sql server (alternate) > december 2004 >

sql server (alternate) : Help with NOT EXISTS query


nib
12/13/2004 2:32:49 PM
[quoted text, click to view]

SELECT <select list>
FROM Customers c
WHERE NOT EXISTS (SELECT *
FROM Addresses a
WHERE c.Cus_No = a.Cust_No
AND c.ADR_CD = a.ADR_CD)

Ross Presser
12/13/2004 3:32:04 PM
[quoted text, click to view]

SELECT Customers.cus_no, Customers.adr_cd
FROM Customers
WHERE NOT EXISTS
(SELECT *
FROM ADDRESSES
WHERE Customers.cus_no = ADDRESSES.cus_no
AND Customers.adr_cd = Addresses.adr_cd )

Alternative method:

SELECT Customers.cus_no, Customers.adr_cd
FROM Customers
LEFT JOIN Addresses
ON Customers.cus_no = Addresses.cus_no
AND Customers.adr_cd = Addresses.adr_cd
WHERE Addresses.cus_no IS NULL

RDRaider
12/13/2004 8:17:45 PM
Newbie here...looking for help with a NOT EXISTS query or suggestions for a
better method.
I have 2 linked tables: CUSTOMERS and ADDRESSES
common fields are CUS_NO and ADR_CD
I need to find records where an address code (ADR_CD) entered into CUSTOMERS
does not have that same ADR_CD existing in the ADRESSES table.

Example:
CUS_NO = 12345
ADR_CD = Ohio01

If the combination of cus_no 12345 and Ohio01 does not exist in the
ADDRESSES table, I need to find them.

Thanks in advance.

RDRaider
12/13/2004 8:51:50 PM
Thanks for the quick reply. It works! I don't know why I couldn't get the
same results, need to hit the books I guess.


[quoted text, click to view]

AddThis Social Bookmark Button