?
Solved

Delete all but one duplicate record in a table

Posted on 2014-11-19
6
Medium Priority
?
1,595 Views
Last Modified: 2014-11-21
I have a table that contains duplicate records.  I want to somehow delete all but one of the duplicate records.  Although it is not the key field, the field that contains the duplicate values is "Account No".  How can I delete all but one of the records?
0
Comment
Question by:SteveL13
[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
6 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40453798
assuming this is the table structure

pkey     accountno
1            abc
2            cde
3            abc
4            abc
5            cde
6            efg

you can try something like this!

delete from tablename 
where pkey in (select list(pkey) from tablename
    group by accountno having count(pkey)>1) 
and pkey not in (select min(pkey) from tablename
    group by accountno having count(pkey)>1)

Open in new window

of course replace tablename by your tablename, pkey with the pkey column and accountno with the account number column name..
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40453873
Assuming you only have the one field [Account No], then you could simply do a group by query and then change it to a make table query.  Much simpler than deleting the duplicates.

However, if you have other fields, it becomes more difficult.  How do you know which of the duplicates to delete?  Do you only want to keep the most recent record for each [Account No] field, if so, how do you determine "most recent".
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40454019
I have a table that contains duplicate records
One assumes they are complete duplicates and there is nothing to differentiate them.

Throw a button on a form.
Code behind the button

Private Sub cmdRemoveAllButTwo_Click()
dim rs as Recordset
dim rs1 as recordset
set rs = Currentdb.OpenRecordset("Select Distinct [Account No] from ThatTable", dbopendynaset,dbseechanges)
'great, we have all the Account Nos
'Now walk down that set

Do until rs.eof
    set rs1 = Currentdb.OpenRecordset("Select * from ThatTable wehere  [Account No] =" & rs! [Account No] , dbopendynaset,dbseechanges)
    if rs1.RecordCount < 3 then
        goto done
    else
        rs1.movenext 'skip one
        rs1.movenext 'skip two
        do until rs1.eof
              rs1.delete 'kill it
              rs1.movenext
        loop
    end if
done:
    rs1.close    
    set rs1 =nothing
    rs.movenext
loop

msgbox "done!"

end sub

It's air-code, but it ought to do the trick.
Get all the [Account No] values
Open a recordset of the table where the [account no] equals each value in turn.
Skip two if there are that many.
Delete the rest
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 40454498
The good old method:

Make sure to make a backup and test it first. . .

1. Add an autonumber field if you don't have a PK.
2. Create a totals Query1 which shows GroupBy the name, and First of the PK.
3. Create a delete Query2 with the PK criteria like this:

Not In (Select FirstOfPK From Query1;)

When you run the delete Query2 it will delete everything _except_ one occurrence of each name.

/gustav
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40455105
Can't help you as far as the Access answer, but here's the SQL Server answer on how to pull that off: SQL Server Deleting Duplicate Rows , with lots of code examples and images.   If the article helps you please click the big green 'Was this article helpful?' button at the end.

No idea if Access allows Common Table Expressions (CTE's).  Perhaps if you wish to go down this road one of the Access experts can speak to that.

Good luck.
Jim
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40458653
just a question! did you try my sql?
0

Featured Post

Technology Partners: 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 been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Suggested Courses

764 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