UNION query and keeping one version of duplicates

Bamh1

New Member
Joined
Oct 7, 2021
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello,
I have two employee tables with similar structure imported in Ms Access. Table A contains permanent employee data, and Table B contractors. Some names appear in both tables. I want to make a union between these two tables such that if employee name appears in both tables, it will only keep the one in Table A. That is, if a record of permanent employee also exist as a contractor, the contractor record for that employee will be ignored in the union.

General query form:
Select * FROM TableA
UNION
Select * FROM TableB

Could someone please assist adding a condition, if statement or VBA to achieve the above.

Thank you,
Shawn
 

Attachments

  • Capture1.PNG
    Capture1.PNG
    4.5 KB · Views: 11
  • Capture2.PNG
    Capture2.PNG
    4.6 KB · Views: 10

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try removing status field, then you should get unique records as long as you're not using UNION ALL.
Then create a new query, add the union query and permanent table to this new query and add status field. Hopefully that will produce the result you want.
If you need further help, posting data is better than pictures. With the amount of time I spend on various forums, I usually lack the desire to manually type out data for someone when they could post it.
 
Upvote 0
Try removing status field, then you should get unique records as long as you're not using UNION ALL.
Then create a new query, add the union query and permanent table to this new query and add status field. Hopefully that will produce the result you want.
If you need further help, posting data is better than pictures. With the amount of time I spend on various forums, I usually lack the desire to manually type out data for someone when they could post it.
The
Hello,
I have two employee tables with similar structure imported in Ms Access. Table A contains permanent employee data, and Table B contractors. Some names appear in both tables. I want to make a union between these two tables such that if employee name appears in both tables, it will only keep the one in Table A. That is, if a record of permanent employee also exist as a contractor, the contractor record for that employee will be ignored in the union.

General query form:
Select * FROM TableA
UNION
Select * FROM TableB

Could someone please assist adding a condition, if statement or VBA to achieve the above.

Thank you,
Shawn
Try removing status field, then you should get unique records as long as you're not using UNION ALL.
Then create a new query, add the union query and permanent table to this new query and add status field. Hopefully that will produce the result you want.
If you need further help, posting data is better than pictures. With the amount of time I spend on various forums, I usually lack the desire to manually type out data for someone when they could post it.
The status field can't be removed; it's a required field. I am trying to upload data files, but for some reason this thread only allows me to upload image.
 

Attachments

  • Capture1.PNG
    Capture1.PNG
    4.5 KB · Views: 8
  • Capture2.PNG
    Capture2.PNG
    4.6 KB · Views: 10
  • UnionResult.PNG
    UnionResult.PNG
    6.1 KB · Views: 8
Upvote 0
The union result should look like the third image
 
Upvote 0
The status field can't be removed; it's a required field
Then create a new query, add the union query and permanent table to this new query and add status field
Union sql (name is a reserved word)
SELECT EmplID, fullname from tableA
UNION
SELECT EmplID, fullname from tableB;

Final query
SELECT qryUnion.*, IIf(IsNull([Status]),"Contractor",[Status]) AS EmplStatus
FROM qryUnion LEFT JOIN TableA ON qryUnion.fullname = TableA.FullName;
 
Upvote 0
The union result should look like the third image

Union sql (name is a reserved word)
SELECT EmplID, fullname from tableA
UNION
SELECT EmplID, fullname from tableB;

Final query
SELECT qryUnion.*, IIf(IsNull([Status]),"Contractor",[Status]) AS EmplStatus
FROM qryUnion LEFT JOIN TableA ON qryUnion.fullname = TableA.FullName;
Micron,

Thank you for taking the time to review this request; however, the query that you wrote doesn't seem to work; it's still including both versions of Mary (active and terminated) in the result. I want only the active one kept.
 
Upvote 0
Now it's active vs terminated?
if a record of permanent employee also exist as a contractor,
What I posted seemed to work for me. If each table has 4 records

TableA
EmplIDFullNameStatus
1John DoePermanent
2Jane DoePermanent
3Susan DoePermanent
4Mary DoePermanent

TableB
EmplIDFullNameStatus
1Rob DoeContractor
2Jane DoeContractor
3Cathy DoeContractor
4Lisa DoeContractor

and only one person appears in both, the the result should be 7 records, which is what I got - and the person who's in both only shows up once.

Query1
EmplIDfullnameEmplStatus
1John DoePermanent
1Rob DoeContractor
2Jane DoePermanent
3Cathy DoeContractor
3Susan DoePermanent
4Lisa DoeContractor
4Mary DoePermanent
I guess I don't understand the end goal.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top