Solved

Delete all but one duplicate record in a table

Posted on 2014-11-19
6
1,401 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 (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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 50

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 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

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.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

690 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