Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2004, 07:48 AM
Authorized User
 
Join Date: Apr 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete duplicates?

Hello!

I have a table with a column (nr) that miss a unique index and therefore also contains duplicates. (The table also have an identity column) I use the following question to identify which rows that are duplicates:

select count(nr) as Total, nr
from tTable
group by nr
having count(nr) > 1

The result might look like this
Total nr
3 1

What I would like to know is how I can delete all of the rows with nr = 1 but one row that I would like to keep in the table.

Thanks in advance!

Boson


 
Old August 11th, 2004, 11:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Delete from tTable where nr=1 and ID not in (select min(ID) from tTable where nr = 1)

ID is the identity field. Make sure the nr field is indexed, as that will add to the performance.

Brian
 
Old August 11th, 2004, 04:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

A more general solution is discussed in this article:

http://support.microsoft.com/default...NoWebContent=1

Pay particular attention to the comment the article makes, "...you should closely examine the process which allowed the duplicates to happen in order to prevent a recurrence." ;)


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
php/mysql delete button and delete query dungey PHP Databases 17 April 11th, 2009 12:24 PM
Yes to duplicates MMJiggy62 Beginning VB 6 3 July 14th, 2006 11:51 AM
delete duplicates rows guga121 MySQL 2 June 20th, 2006 12:24 AM
Duplicates ashley_y Access 11 August 14th, 2003 03:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.