Solved

Delete all but one duplicate record in a table

Posted on 2014-11-19
6
945 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
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
just a question! did you try my sql?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now