I need two queries merged/combined

K_Deutsch
K_Deutsch used Ask the Experts™
on
My preference is one query and not two, so I need someone to combine the query named _1 (which pulls SSNs from two different fields) and looks like this

SELECT NAMAST.SOC_SEC_NO_9 AS SSN
FROM MASTER_ALL INNER JOIN NAMAST ON (MASTER_ALL.APP = NAMAST.APP) AND (MASTER_ALL.ACCTNO = NAMAST.ACCTNO)
WHERE (((NAMAST.SOC_SEC_NO_9)<>0) AND ((MASTER_ALL.ALL_CUR_BAL_21)>0));

UNION SELECT NAMAST.SECOND_SSN_213 AS SSN
FROM MASTER_ALL INNER JOIN NAMAST ON (MASTER_ALL.ACCTNO = NAMAST.ACCTNO) AND (MASTER_ALL.APP = NAMAST.APP)
WHERE (((NAMAST.SECOND_SSN_213)<>0) AND ((MASTER_ALL.ALL_CUR_BAL_21)>0));

With this one (which for each SSN pulls a zip code from another table)

SELECT [_1].SSN, CIFHSHLD.CIFH_SEQ, CIFHSHLD.CIFH_ZIP
FROM (_1 INNER JOIN CIFMASTR ON [_1].SSN = CIFMASTR.CIFM_NBR) INNER JOIN CIFHSHLD ON CIFMASTR.CIFM_HSHLD_NBR = CIFHSHLD.CIFH_HSHLD_NBR
WHERE (((CIFHSHLD.CIFH_SEQ)=0))
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
merged/combined - doesn't tell me what you want as the output.  Do you want to join the two queries?  Inner Join or Outer Join?  to add columns.   Or do you want to union the second query with the first to add rows.

To union the third query, add the SQL String into the first query but strip out the extra columns.  In a Union, all columns must align, query to query for all Selects in the Union.

Joining query1 to query2 gives you a third query but I don't know anything about your data so I don't know what type of join you would need.

Author

Commented:
Let's start over and see if this time I provide enough info to get to one query. See attached.
Sample-Table-Data.xlsx
Most Valuable Expert 2012
Top Expert 2014

Commented:
Our role isn't really to do it for you, but rather to help you do it yourself. Pat gave you some very good guidelines for a UNION query - try to put those together, and post back here with the results.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial