tags:

views:

142

answers:

2

I have two tables that have the exact same structure.

Table MasterList Acct_id(9) Name (25) Address (35) City(15) State(2) ZipCode(5)

and

Table NewMasterList Acct_id(9) Name (25) Address (35) City(15) State(2) ZipCode(5)

I need a query that will display the Acct_ID and Name for all records from each table that are NOT in BOTH tables.

+3  A: 

Assuming acct_ID is the primary key:

SELECT COALESCE(ml.acct_ID, nml.acct_ID) AS AccountID
     , COALESCE(ml.name, nml.name) AS Name 
FROM MasterList ml
    FULL OUTER JOIN NewMasterList nml ON nml.Acct_ID=ml.Acct_ID
WHERE nml.acct_ID IS NULL OR ml.acct_ID IS NULL
Joel Coehoorn
This query worked as well as the second query presented. Each returned the correct data, although the second one using the union with the nested selects was a bit easier to understand at my level.
Keniwan
This one _should_ be a lot faster, but that probably doesn't matter: this is the kind of thing you do once for analysis.
Joel Coehoorn
+1  A: 

Hi

I presume the following query should help-

select Acct_id,Name 
from MasterList 
where Acct_id not in (select distinct Acct_id from NewMasterList) 

union 

select Acct_id,Name 
from NewMasterList 
where Acct_id not in (select distinct Acct_id from MasterList)

cheers

Andriyev
Although both answers presented returned the same and correct results, I selected this one as it is a bit easier to understand, at least at my level for now.
Keniwan