Solved

Delete all but one duplicate record in a table

Posted on 2014-11-19
6
1,115 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
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 47

Expert Comment

by:Dale Fye (Access MVP)
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 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 65

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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