• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Delete duplicates from table based on 3 fields

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
Fordraiders
Asked:
Fordraiders
  • 2
  • 2
  • 2
  • +2
1 Solution
 
dqmqCommented:
>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
 
FordraidersAuthor Commented:
delete first duplicateby deleting the first duplicate...
0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
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
Independent Software Vendors: 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!

 
hnasrCommented:
How can you tell first from second record of a set of duplicate records?
0
 
hnasrCommented:
If you set primary key to the table, then you avoid duplicates.
0
 
Rey Obrero (Capricorn1)Commented:
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
 
dqmqCommented:
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
 
FordraidersAuthor Commented:
Thanks Cap
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now