Solved

Delete Duplicate query. Deletes duplicates. But I need for it to keep one record once it deletes

Posted on 2013-01-25
7
277 Views
Last Modified: 2013-02-12
Hello, Experts
I have a query that finds the duplicates and deletes them. This works fine. However, it deletes all the duplicates of a certain record. I need for it once it finds a duplicate delete it but just keep one of the records.

Meaning delete all but one, once you find the duplicates.

DELETE [Serial ].*, tblInductionCheckHistory.Serial
FROM tblInductionCheckHistory
WHERE (((tblInductionCheckHistory.Serial) In (SELECT [Serial] FROM [tblInductionCheckHistory] As Tmp GROUP BY [Serial] HAVING Count(*)>1 )));
0
Comment
Question by:gigifarrow
  • 3
  • 3
7 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38819138
are you deleting the duplicates except one in tblInductionCheckHistory ?


delete *
from tblInductionCheckHistory
where [serial] not in(select min(a.[serial]),[otherfieldwithduplicates] from tblInductionCheckHistory
group by tblInductionCheckHistory.[otherfieldwithduplicates])
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38819139
If you provide some sample data with tabe fields and desired result in excel format ... it will help :-)
0
 

Author Comment

by:gigifarrow
ID: 38826985
Okay here is the table and the query it deletes all the records that have duplicates. I need for it to delete all but one.




example;

serial numbers with duplicates
2334544
2334544
2334544
2468246

wants duplicates deleted it looks like this:
2334544
2468246
Helpdeleteduplicates.zip
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:gigifarrow
ID: 38827017
I dont understand where it says [otherfieldwithduplicates]
what is suppose to be in this field.?

What does the (a) mean in right here:(select min(a.[serial]),[otherfieldwithduplicates]

from tblInductionCheckHistory
where [serial] not in(select min(a.[serial]),[otherfieldwithduplicates] from tblInductionCheckHistory
group by tblInductionCheckHistory.[otherfieldwithduplicates])
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 38827042
you need to add an autonumber field to your table

see this revised db

see query1
Helpdeleteduplicates.accdb
0
 

Author Comment

by:gigifarrow
ID: 38831507
Thank you for your help how come you dont have to Dim a and b. If you have time could you please explain. Just want to understand. So I can do it on my own.

Works great!!
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38831515
<how come you dont have to Dim a and b>

in a query, you don't need to use DIM

you can use

from tableName AS T

or simply

from TableName  T
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

705 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

18 Experts available now in Live!

Get 1:1 Help Now