Unique duplicate records from 2 tables (1 Viewer)

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18
I am trying to compare 2 tables "Customers" and "CustomersNew" to find matching addresses.

The objective is to identify an address that we have worked at previously.

I have been trying the below code
Code:
SELECT DISTINCT Customers.OrderDate, Customers.Address
FROM Customers RIGHT JOIN Customers_New ON Customers.Address = Customers_New.Address
ORDER BY Customers.Address;

This gives the desired results, but also includes other records from "CustomersNew" please refer to attached file.

Is there a way of just returning the duplicate records only.
 

Attachments

  • DuplicateAddresses.accdb
    712 KB · Views: 404

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18
Hi June7 thanks for the reply
Code:
SELECT DISTINCT Customers.OrderDate, Customers.Address
FROM Customers INNER JOIN Customers_New ON Customers.Address = Customers_New.Address
ORDER BY Customers.Address;
I still get the same results
3 unmatched results
 

June7

AWF VIP
Local time
Today, 02:32
Joined
Mar 9, 2014
Messages
5,466
What do you expect to show?

The query is matching addresses. You get 'duplicates' because one of the tables has multiple records for same address. The query is doing what it is designed to do but apparently not what you want.

Using DISTINCT does nothing because the OrderDate field makes each record unique.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:32
Joined
May 21, 2018
Messages
8,525
Is this what you want?

QryMatchingAddress QryMatchingAddress

AddressNewOrderDatePreviousOrderDate
457 BALAKLAVA RD, SINGLETONS MILL
28/07/2021​
27/07/2020​
78 MINTARO RD, DURAL
28/07/2021​
1/01/2021​
78 MINTARO RD, DURAL
28/07/2021​
2/02/2021​
78 MINTARO RD, DURAL
28/07/2021​
3/03/2021​
78 MINTARO RD, DURAL
28/07/2021​
4/04/2021​
798 LIEBIGS RD, COTTAGE POINT
28/07/2021​
1/01/2021​
798 LIEBIGS RD, COTTAGE POINT
28/07/2021​
2/02/2021​
798 LIEBIGS RD, COTTAGE POINT
28/07/2021​
3/03/2021​
913 KOONOONA RD, COASTERS RETREAT
28/07/2021​
1/01/2021​
913 KOONOONA RD, COASTERS RETREAT
28/07/2021​
2/02/2021​
913 KOONOONA RD, COASTERS RETREAT
28/07/2021​
3/03/2021​
Code:
SELECT DISTINCT Customers.Address, Customers_New.OrderDate AS NewOrderDate, Customers.OrderDate AS PreviousOrderDate
FROM Customers, Customers_New
WHERE (((Customers.Address)=[Customers_New].[Address]) AND ((Customers.OrderDate)<>[Customers_New].[OrderDate]))
ORDER BY Customers.Address, Customers_New.OrderDate;
 

isladogs

MVP / VIP
Local time
Today, 11:32
Joined
Jan 14, 2017
Messages
18,210
Similar idea to the above but using an inner join rather than a Cartesian join (so should be faster)

Code:
SELECT Customers.OrderDate, Customers.Address
FROM Customers INNER JOIN Customers_New ON Customers.Address = Customers_New.Address
WHERE (((Customers_New.OrderDate)<>[Customers].[OrderDate]))
ORDER BY Customers.Address;

This gives exactly the same results as the above post

Or omit the OrderDate field from the query results
Code:
SELECT DISTINCT Customers.Address
FROM Customers INNER JOIN Customers_New ON Customers.Address = Customers_New.Address
WHERE (((Customers_New.OrderDate)<>[Customers].[OrderDate]))
ORDER BY Customers.Address;

This gives just 4 records without duplication
1627488102981.png
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:32
Joined
Feb 19, 2002
Messages
43,233
Does this do what you want?

SELECT Customers.CustomerName, Customers.Address, Count(*)
FROM Customers INNER JOIN Customers_New ON Customers.Address = Customers_New.Address
Group By Customers.CustomerName, Customers.Address
ORDER BY Customers.CustomerName,Customers.Address;
 

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18
Thank you for the replies.

Appologies for the late response, been a bit busy

isladogs

Your code is the solution that I require,
Code:
SELECT Customers.OrderDate, Customers.Address
FROM Customers INNER JOIN Customers_New ON Customers.Address = Customers_New.Address
WHERE (((Customers_New.OrderDate)<>[Customers].[OrderDate]))
ORDER BY Customers.Address;

The objective is to run a query that will find matching addresses and show the history of work done at the address.

Is there a way of using the LIKE function as I have an issue where most of the addresses have comas, but occasionally an address doesn't have comas, isladogs code works perfectly but misses the addresses without comas.

Thanks again for all the replies.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:32
Joined
May 7, 2009
Messages
19,230
majp has same query on post#5, don't you think?
 

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18

arnelgp

I looked at majp's post again and yes you are correct, it is very similar, isladogs comment "Similar idea to the above but using an inner join rather than a Cartesian join (so should be faster)" caught my eye and followed that suggestion.

I just created a query in my DB with majp's code this also works perfectly, but still have the problem with the comas.

I am curious about using the LIKE function is it usable in majp or isladogs codes.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:32
Joined
May 7, 2009
Messages
19,230
you can use Like on your Criteria, either on 1 table or both tables.
 

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18

arnelgp

Thanks for the reply.

Not sure if I did this correctly but it works:
In the next query cell I entered "Expr1: [Customers].[Address] Like [Customers_New].[Address]"

Thanks again for the help.
 

isladogs

MVP / VIP
Local time
Today, 11:32
Joined
Jan 14, 2017
Messages
18,210
The queries look for matching addresses in both tables. If an address has no commas in both tables it will still match.
If an address has commas in one table but not the other, Access sees them as different...just as it would if the spelling changed.
Presumably commas could be missing in any record in either table...

One way of handling that would be to make two additional queries, each with an extra field Address1 with commas removed
1. qryCustomers
Code:
SELECT Customers.*, Replace([Address],",","") AS Address1
FROM Customers;

2. qryCustomersNew
Code:
SELECT Customers_New.*, Replace([Address],",","") AS Address1
FROM Customers_New;

Now make a third query based on those and joining by the Address1 field
Code:
SELECT qryCustomers.OrderDate, qryCustomers.Address1
FROM qryCustomers INNER JOIN qryCustomersNew ON qryCustomers.Address1 = qryCustomersNew.Address1
WHERE (((qryCustomers.OrderDate)<>[Customers_New].[OrderDate]))
ORDER BY qryCustomers.OrderDate, qryCustomers.Address1;

Or omitting the OrderDate field and using DISTINCT to avoid duplicate addresses (better in my opinion):
Code:
SELECT DISTINCT qryCustomers.Address1
FROM qryCustomers INNER JOIN qryCustomersNew ON qryCustomers.Address1 = qryCustomersNew.Address1
WHERE (((qryCustomers.OrderDate)<>[Customers_New].[OrderDate]))
ORDER BY qryCustomers.Address1;


Not very elegant but it will work. There may be a simpler solution. If anyone else wants to suggest an alternative, that's fine by me.
Do bear in mind that doing this is a 'band aid' to handle bad data.

However none of that will help if the address spellings differ in any way. That will definitely happen due to human error. Obviously a much better solution would be to ensure neither commas or spelling were an issue by using a unique number ID field for each address and using that in your table join instead.

I suggest you change your table structure to reduce the risk of these issues.
For example, store the addresses in a separate table tblAddresses each with an auto number AddressID primary key field.
You would also be better splitting the address into several separate fields
I would also have a unique CustomerID autonumber field and store First name and last name in separate fields
 
Last edited:

King21

New member
Local time
Today, 20:02
Joined
Jun 14, 2021
Messages
18
Thanks for the reply isladogs.
Thanks for the "Band Aid" solution

store the addresses in a separate table tblAddresses each with an auto number AddressID primary key field.
I'll get working on the suggestion.

Thanks again
 

isladogs

MVP / VIP
Local time
Today, 11:32
Joined
Jan 14, 2017
Messages
18,210
A wise decision!
Come back if you have any questions
 

Users who are viewing this thread

Top Bottom