K_Deutsch
asked on
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nailed it, and NOT IN will come in handy for the future. Many thanks.
Open in new window