Solved

Delete duplicates from table based on 3 fields

Posted on 2012-04-09
8
248 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
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

810 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