Solved

Delete all but one duplicate record in a table

Posted on 2014-11-19
6
1,037 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
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…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

864 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

24 Experts available now in Live!

Get 1:1 Help Now