Solved

Delete duplicates from table based on 3 fields

Posted on 2012-04-09
8
246 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 42

Expert Comment

by:dqmq
Comment Utility
>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
Comment Utility
delete first duplicateby deleting the first duplicate...
0
 
LVL 21
Comment Utility
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
 
LVL 30

Expert Comment

by:hnasr
Comment Utility
How can you tell first from second record of a set of duplicate records?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:hnasr
Comment Utility
If you set primary key to the table, then you avoid duplicates.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Cap
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now