473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Deleting Duplicates

Used the "find duplicates" wizard to identify approx 500 duplicates in a
single table. How do you delete the duplicates without doing it one at a
time?
Nov 19 '05 #1
1 1491
"Brian Keanie" <bk******@sympatico.ca> wrote in message
news:fq*******************@news20.bellglobal.com.. .
Used the "find duplicates" wizard to identify approx 500 duplicates in a
single table. How do you delete the duplicates without doing it one at a
time?


Does the table in question have a primary key? If it does, you can use a
delete query like the one in the following example scenario,
A table named MyTable, with a primary key field named MyKey, and duplicate
values in fields MyField1 and MyField2:

DELETE T1.*
FROM MyTable T1
WHERE T1.MyKey<
(SELECT Max(T2.MyKey)
FROM MyTable T2
WHERE T2.MyField1=T1.MyField1
AND T2.MyField2=T1.MyField2;);

If the table in question does NOT have a primary key, however, it becomes
necessary to employ a Visual Basic Sub with QueryDef and Recordset objects
to perform the task. Take the above scenario, without a primary key. The
Visual Basic Sub might look something like this:

' ----------<< begin code >>----------
Public Sub DeleteDuplicates()

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT T1.* " _
& "FROM MyTable T1 " _
& "WHERE EXISTS " _
& " (SELECT T2.MyField1, T2.MyField2 " _
& " FROM MyTable T2 " _
& " WHERE T2.Myfield1=T1.Myfield1 " _
& " AND T2.Myfield2=T1.Myfield2 " _
& " GROUP BY T2.MyField1, T2.MyField2 " _
& " HAVING COUNT(T2.*)>1; " _
& " )" _
& ";")

Do While Not rs.EOF
rs.DELETE
rs.Requery
Loop

rs.Close
Set rs = Nothing

End Sub

' -----------<< end code >>-----------
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ScottH | last post by:
I was looking for thw SQL to delete dupes from a table, and came across this. All who saw it agreed in principle, but I can't quite figure out the logic. If we are deleting all rows whose rowid...
4
by: Jerry Barnes | last post by:
Suppose that I have a table that contains a lot of records that are identical except for an id field and a date-time-stamp field. For example Id Unit Price DTS 1 A 1.00 Date 1 2 ...
18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
2
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something like this: unid | oid ---------+---------
19
by: MaXX | last post by:
Hi, I hope I'm not OT. I have the following issue: I want to delete a record from my db with a php script. Let's say I'm auth'd and I want to delete the record id 440. With a simple form (get...
3
by: Maury | last post by:
Hello, I have a stored procedure that deletes duplicates in one table: ..... AS BEGIN DELETE FROM mytable WHERE Id IN( SELECT Max(id)
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
1
by: chrisclarke84 | last post by:
Hi, Is it possible to delete only 1 record when you have 2 duplicate records in a table. Normally i would do a group by query, but in some cases there are records where i want duplicates and also...
4
by: N2Deep | last post by:
I have a table named SUPPORT DATA, I have a field named Serial Number. In the Serial Number field I have many duplicates, and I only want one of each. Sample serials ABB045000MG, JBX05050016 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.