How to find matching records comparing one field in a table to several fields in another table?
Using MS Access 2010, I have two tables where I want to find the matching records in one of them by comparing similar fields (1:1 and 1:M).
Table 1, [760Vadim] has four fields: Folio, PID Number1, PID Number2, PID Number3
Table 2, [no_dupes] has two fields: Folio, PID
I need to find all records in [760Vadim] that have a matching value from the similar fields in [no_dupes].
Comparing Folio fields is a 1:1 join whereas comparing the PIDs is 1:M (1:3) and they both need to be compared at the same time!
I have tried this using a Join and the following SQL statements:
1.) Works, but only for one field at a time!
SELECT [760Vadim].*, no_dupes.PID
FROM no_dupes INNER JOIN 760Vadim ON no_dupes.PID = [760Vadim].[PID Number1]
WHERE (((no_dupes.PID) Like [760Vadim].[PID Number1]));
2.) Does not work when comparing PID to two fields, 0 records returned...!
SELECT [760Vadim].*, no_dupes.PID
FROM no_dupes INNER JOIN 760Vadim ON (no_dupes.PID = [760Vadim].[PID Number2]) AND (no_dupes.PID = [760Vadim].[PID Number1])
WHERE (((no_dupes.PID) Like [760Vadim].[PID Number1])) OR (((no_dupes.PID) Like [760Vadim].[PID Number2]));
3.) This kind of works... but returns duplicates... and going back into design view, it says invalid operator due to changing AND to OR, so it doesn't like my = sign?
SELECT [760Vadim].*, no_dupes.PID
FROM no_dupes INNER JOIN 760Vadim ON (no_dupes.PID = [760Vadim].[PID Number2]) OR (no_dupes.PID = [760Vadim].[PID Number1])
WHERE (((no_dupes.PID) Like [760Vadim].[PID Number1])) OR (((no_dupes.PID) Like [760Vadim].[PID Number2]));
Also tried another forums suggestions: 1.) Works (i think...), but duplicates again!
SELECT [760Vadim].*
FROM 760Vadim, no_dupes
WHERE ((([760Vadim].folio)=[no_dupes].[DATA_SO1])) OR ((([760Vadim].[PID Number1])=[no_dupes].[PID])) OR ((([760Vadim].[PID Number2])=[no_dupes].[PID])) OR ((([760Vadim].[PID Number3])=[no_dupes].[PID]));
It seems to go through and return a record if a match is found for folio, AND returns another record (could be a duplicate) if a matching PID is found from the three other fields.
From a similar question it is suggested to use the CONCAT feature inside of the Join to remove the duplicates.
I also tried adding: GROUP BY [760Vadim].*;
after the where statement to remove duplicates but no avail, it says cannot group ALL records, which is what I need. I am new to Access but have some familiarity with SQL (basics).
Apologies if posted in wrong stack, it may sit better in Programmers or Database stackexchange?
EDIT: I tried the option without the Join as:
SELECT DISTINCT v.*
FROM [760Vadim] v
WHERE
EXISTS(SELECT * FROM no_dupes nd
WHERE nd.pid LIKE v.[PID Number1]
OR nd.pid LIKE v.[PID Number2]
OR nd.pid LIKE v.[PID Number3]
OR nd.folio LIKE v.[folio] )
The option you provided only returns the four columns from the 760Vadim table, it's preferred to see ALL of them. I also noticed it didn't compare the folio fields (that specification is lost in my ramble, sorry).
Did I format it correctly to SELECT DISTINCT
all columns in 760Vadim IF a match is found in no_dupes?
When I go back into SQL/design view, it throws an exception error and changes the SQL to:
SELECT DISTINCT v.* INTO query_final
FROM 760Vadim AS v
WHERE (((Exists (SELECT * FROM no_dupes nd
WHERE nd.pid LIKE v.[PID Number1]
OR nd.pid LIKE v.[PID Number2]
开发者_运维技巧 OR nd.pid LIKE v.[PID Number3]
OR nd.folio LIKE v.[folio] ))<>False));
You have a couple options
Using DISTINCT/JOIN
SELECT DISTINCT v.folio,
v.[PID Number1],
v.[PID Number2],
v.[PID Number3]
FROM [760Vadim] v
INNER JOIN no_dupes nd
ON nd.pid LIKE v.[PID Number1]
OR nd.pid LIKE v.[PID Number2]
OR nd.pid LIKE v.[PID Number3]
USING DISTINCT/EXISTS
SELECT DISTINCT
v.folio,
v.[PID Number1],
v.[PID Number2],
v.[PID Number3]
FROM [760Vadim] v
WHERE
EXISTS(SELECT * FROM no_dupes nd
WHERE nd.pid LIKE v.[PID Number1]
OR nd.pid LIKE v.[PID Number2]
OR nd.pid LIKE v.[PID Number3] )
精彩评论