Deleting Duplicate records from a query result

Shallo9

Registered User.
Local time
Yesterday, 18:23
Joined
Nov 11, 2011
Messages
95
Hello All,

I have a query which returns a vast number of records. Now I want to delete any duplicate record (Leaving the first instance).

I can manually copy this resultant data to excel and delete the duplicates easily by using the Remove Duplicates Option. I want this functionality to be incorporated into my query.

NB: I do not want the records to be deleted from the table.


Any help would be highly appreciated.


Many Thanks
 
perhaps
SELECT DISTINCT ........
or
GROUP BY
query.

Thanks
 
Already tried with no success.

I've got a PK & a FK in this query result and want to remove the duplicate records based on the FK.
 
on the fly, just check if below gives some guidelines :

Code:
SELECT 
	Table1.TestID_PK, 
	Table1.TestName, 
	First(Table2.TestID1_PK) AS FirstOfTestID1, 
	Table2.TestID_FK, 
	First(Table2.TestField2) AS FirstOfTestField2
FROM 
	Table1 
	INNER JOIN 
	Table2 
	ON 
	Table1.TestID_PK = Table2.TestID_FK
GROUP BY 
	Table1.TestID_PK, 
	Table1.TestName, 
	Table2.TestID_FK;

perhaps, if you post the query or the tables involved with some dummy data thrown in & the result you want out of the query, someone should be able to help you.

Thanks
 
Code:
SELECT tblActivityContact.ActivityID, tblActivityContact.activityContactID, tblActivityContact.contactTypeID, tblActivityContact.contactReasonTypeID
FROM tblActivityContact
WHERE (((tblActivityContact.contactTypeID) In (1,4,5)) AND ((tblActivityContact.contactReasonTypeID) In (19,25,26,27)))
ORDER BY tblActivityContact.activityContactID;

tblActivityContact.ActivityID is the FK which has duplicates
 
Can you post a few query results and point out the duplicates?
 

Attachments

  • Dups.JPG
    Dups.JPG
    27.4 KB · Views: 182
If you are looking to delete/not select duplicates, you must have a clear understanding of what a duplicate is.

With your code
Code:
SELECT ActivityID
, activityContactID
, contactTypeID
, contactReasonTypeID
FROM tblActivityContact
WHERE
contactTypeID In (1,4,5) AND 
contactReasonTypeID In (19,25,26,27)
ORDER BY activityContactID;
a duplicate records would have the same values in all of the fields.

I see differences in the fields of the records you are calling duplicates.

For the 109 ActivityId record, the contactTypeIds are different.
For activityID 752 the values for ContactReason are different.

So these are NOT duplicates in Access terms.

Also , the field in your query result jpg, do not match the field names in the query??

Are you using Lookups at the table level??
 
Yes they are lookup values. Basically every Activity has Many ContactID. I've sorted the result of the query so that I can keep the first contact and delete the rest.
 
I think you've completely missed my point. From my query above I get All the records which satisfies my criteria. However the next step is, I need Only 1 record per Activity and where there are more than one records for a given activity I'd sort them by Contact ID and pick the record with the smallest value(Which in other sense would be the first contact). (I can do this with the Remove Duplicates option in Excel would do) My duplicate value is in the Activity ID Column.
 
I've probably misunderstood some of it for sure. I was confused with your "duplicates" title and the jpg you sent showing the duplicates.

Anyway, if you create a table as per the sample video that Alan suggested post #7 and take your existing query and the current results, the resultant table will have the records you want. The duplicates will show up as key violations and will not be in the table.

You can use the table for whatever you want (query/report) , then discard it or keep it as you wish.
 
I have a similar (but different) problem with duplicates. I have a query that compares two tables. The results I'm look for are comparison of activities with supervisors and their team members. I'm looking for activity from any one of a superviors team members. To do this, I want the query to delete any instances of where two or more team members have the same supervisor so the result for that supervisor would be one or none. I've tried the SELECT DISTINCT with no luck. Here is the query-

SELECT Count(Full.[Supervisor Alias]) AS [CountOfSupervisor Alias]
FROM [Full] INNER JOIN TBL_AgentInstallations_all ON Full.[Supervisor Alias] = TBL_AgentInstallations_all.[Agent Alias]
WHERE (((Full.[Status Effect Date])<=([TBL_AgentInstallations_all].[Install Compl]+1)));

Business application-To determine who has completed training on or before the application was installed. "Full" is the training completion data for everyone. The team members can only be identified by their supervisor alias. "TBL_AgentInstallations_all" is the installation data for supervisors only (agents).

Thanks,
Dennis
 
Please post your database - remove anything private/confidential. I'm sure someone will look.
 
This my sandbox copy. It has some in-progress objects that I will delete later.;)

Thanks in advance!
Dennis
 

Attachments

Thank you. I've added a relationship (Option 3) "Include ALL records from 'TBL_AgentInstallations_all' and only those records from 'Full' where the joined fields are equal."
 
Glad you have it working.

Setting up relationships and turning Referential Integrity On will help you with your databases.
 
It's not working, but at least, there is a relationship now:banghead:
 

Users who are viewing this thread

Back
Top Bottom