Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete duplicates from table based on 3 fields

Posted on 2012-04-09
8
Medium Priority
?
254 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 31

Expert Comment

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

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

618 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