Query which joins a table only to exclude records?

This query currently works:

SELECT NAMES.LEGAL_NAME1_215, NAMES.LEGAL_NAME2_216, ACCTS.ACCTNO, NAMES.ADDRESS_3, NAMES.ADDRESS_4, NAMES.ADDRESS_5, NAMES.STATE_6, NAMES.CITY_6, NAMES.ZIP_CODE_7, ACCTS.CYCLE_48, ACCTS.DSC_CUR_BAL_21
FROM ACCTS INNER JOIN NAMES ON (ACCTS.APP = NAMES.APP) AND (ACCTS.ACCTNO = NAMES.ACCTNO)
WHERE (((NAMES.ADDRESS_5) Like "1234*") AND ((ACCTS.CYCLE_48)=100 Or (ACCTS.CYCLE_48)=200) AND ((ACCTS.BAL)>0))

There is a third table named NAMES_ALT (NAMES_ALT.ACCTNO can join to NAMES.ACCTNO) that I want to use only so I can exclude certain records. Specifically, any records where NAMES_ALT.USED is "Y" AND NAMES_ALT.STOP is Null or greater than 10/5/2015 and NAMES_ALT.CODE is "A" OR "S".

Would like to do this all in one query. Will someone please modify my working query to include the third table exclusions?
K_DeutschAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasMcA2Commented:
SELECT NAMES.LEGAL_NAME1_215, NAMES.LEGAL_NAME2_216, ACCTS.ACCTNO, NAMES.ADDRESS_3, NAMES.ADDRESS_4, NAMES.ADDRESS_5, NAMES.STATE_6, NAMES.CITY_6, NAMES.ZIP_CODE_7, ACCTS.CYCLE_48, ACCTS.DSC_CUR_BAL_21
FROM ACCTS INNER JOIN NAMES ON (ACCTS.APP = NAMES.APP) AND (ACCTS.ACCTNO = NAMES.ACCTNO)
WHERE (((NAMES.ADDRESS_5) Like "1234*") AND ((ACCTS.CYCLE_48)=100 Or (ACCTS.CYCLE_48)=200) AND ((ACCTS.BAL)>0))
AND NAMES.ACCTNO NOT IN (SELECT ACCTNO FROM NAMES_ALT WHERE NAMES_ALT.ACCTNO = NAMES.ACCTNO AND NAMES_ALT.USED = "Y" AND (NAMES_ALT.STOP is Null OR NAMES_ALT.STOP > #10/5/2015#) AND NAMES_ALT.CODE IN ("A", "S"))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
or using a Left Join, like:
SELECT NAMES.LEGAL_NAME1_215, NAMES.LEGAL_NAME2_216, ACCTS.ACCTNO, 
NAMES.ADDRESS_3, NAMES.ADDRESS_4, NAMES.ADDRESS_5, NAMES.STATE_6, NAMES.CITY_6, NAMES.ZIP_CODE_7, 
ACCTS.CYCLE_48, ACCTS.DSC_CUR_BAL_21

FROM ACCTS INNER JOIN NAMES ON (ACCTS.APP = NAMES.APP) AND (ACCTS.ACCTNO = NAMES.ACCTNO)
LEFT JOIN 
(
	SELECT ACCTNO FROM NAMES_ALT 
	WHERE NAMES_ALT.USED = "Y" AND (NAMES_ALT.STOP is Null OR NAMES_ALT.STOP > #10 May 2015#) AND NAMES_ALT.CODE IN ("A", "S")
) AS NAMES_ALT ON NAMES.ACCTNO = NAMES_ALT.ACCTNO

WHERE (((NAMES.ADDRESS_5) Like "1234*") AND ((ACCTS.CYCLE_48)=100 Or (ACCTS.CYCLE_48)=200) AND ((ACCTS.BAL)>0))
AND NAMES_ALT.ACCTNO IS NULL

Open in new window

0
K_DeutschAuthor Commented:
Nailed it, and NOT IN will come in handy for the future. Many thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.