Solved

Delete duplicates from table based on 3 fields

Posted on 2012-04-09
8
251 Views
Last Modified: 2012-04-15
Access 2003

1 databases
db_Data1


db_Data1
1 table:  tblCurrent
4 fields
Item
Mrfnum
Mfgname
Xtype


Item, Mfrnum, Mfgname  are unique.

if I have duplicates on those 3 fields...delete the first record...not the second record.


Thanks
fordraiders





I have a linked table:
from db_Data1 to db_Data2 Linked to tblNew
What I need.
Weekly.mdb
0
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 37825643
>if I have duplicates on those 3 fields...delete the first record...not the second record.


What do you mean by the "first" record?   Seriously, I mean "first" by what ordering?  There is no "first" record in the absolute sense.
0
 
LVL 3

Author Comment

by:fordraiders
ID: 37825865
delete first duplicateby deleting the first duplicate...
0
 
LVL 21
ID: 37825886
I like to use a sub query. See: http://www.allenbrowne.com/subquery-01.html

From the above link

Delete duplicate records

This example uses a subquery to de-duplicate a table. "Duplicate" is defined as records that have the same values in Surname and FirstName. We keep the one that has the lowest primary key value (field ID.)
DELETE FROM Table1
WHERE ID <>  (SELECT Min(ID) AS MinOfID FROM Table1 AS Dupe      
   WHERE (Dupe.Surname = Table1.Surname)                         
   AND (Dupe.FirstName = Table1.FirstName));       

Open in new window

             

Nulls don't match each other, so if you want to treat pairs of Nulls as duplicates, use this approach:
DELETE FROM Table1
WHERE ID <>  (SELECT Min(ID) AS MinOfID FROM Table1 AS Dupe      
   WHERE ((Dupe.Surname = Table1.Surname)                        
     OR (Dupe.Surname Is Null AND Table1.Surname Is Null))       
   AND ((Dupe.FirstName = Table1.FirstName)                      
     OR (Dupe.FirstName Is Null AND Table1.FirstName Is Null))); 

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 30

Expert Comment

by:hnasr
ID: 37826005
How can you tell first from second record of a set of duplicate records?
0
 
LVL 30

Expert Comment

by:hnasr
ID: 37826007
If you set primary key to the table, then you avoid duplicates.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 37826087
take your pick on these queries


DELETE todelete.*
FROM todelete
Where comments In(select min(x.comments) from todelete as X where X.item=todelete.item and x.mfrnum=todelete.mfrnum  and x.mfgname=todelete.mfgname)

or this

DELETE todelete.*
FROM todelete
Where comments In(select max(x.comments) from todelete as X where X.item=todelete.item and x.mfrnum=todelete.mfrnum  and x.mfgname=todelete.mfgname)
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37828389
I hope you don't have occasions of the same comment in both records :>(.  Perhaps safer to use "not in (..."

But, really, I still have concerns about the viability of these solutions against a larger data sample.   And the issue surrounds clarifying what is meant by "first" in a way SQL can understand.  There's an important lesson involved:  rows are inherently unordered.  For a reliable outcome you need to express the ordering in terms of data contained in the rows.

Perhaps you are not revealing other columns the can be used to convey the first-second ordering
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 37848842
Thanks Cap
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question