Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 29th, 2003, 05:54 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deleting Duplicates

Hi - I have uploaded into a temp table approx. 300K of records, within this table there are 2 duplicate records. I want to delete these before I insert them into a base table and put a PKey on Col1

Col1
X0001
Y0000
X0001
Y0000

It does not matter which one of the duplicate records I delete. How can I do this with SQL??

TIA
Ian
 
Old August 29th, 2003, 06:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Since you know that you only have 1 duplicate of each value, you can just use the ROWCOUNT option to prevent it deleting more than one row, like this:
Code:
SET ROWCOUNT 1
DELETE FROM <<tablename>>
WHERE Col1='X0001'
DELETE FROM <<tablename>>
WHERE Col1='Y0000'
 
Old August 29th, 2003, 06:25 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply unfortunately, the duplicate values could change next time I update my base table, So a dummy scenario could be no duplicate values next time or 1 or more duplicates.
 
Old August 29th, 2003, 06:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

OIC you just want a general solution to this problem. The usual method is to
1. select distinct records into a temp table,
2. clear the original table
3. copy records back from temp table to original table

I'm sure you'll find plenty of examples if you search google with something like "SQL Server delete duplicate"

rgds
Phil
 
Old August 29th, 2003, 06:44 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Phil
This makes sense

Regards
Ian
 
Old August 31st, 2003, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

If your temporary table has a primary key column, then there is an alternative to copying the distinct rows into a temp table, deleting the source table then copying things back.

Let's assume you assigned an identity column as the primary key ('keycol' in my code below), and let's further assume you'd be happy to retain the first (lowest primary key value) row for any duplicates:
Code:
DELETE FROM YourTable
    WHERE keycol NOT IN
         (SELECT MIN(keycol)
            FROM YourTable
            GROUP BY Col1);
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicates hewstone999 Access VBA 2 November 4th, 2008 05:26 PM
Yes to duplicates MMJiggy62 Beginning VB 6 3 July 14th, 2006 11:51 AM
eliminating all but 1 of duplicates nlicata SQL Server 2000 3 November 21st, 2003 04:52 PM
Deleting duplicates prabodh_mishra SQL Server 2000 3 September 23rd, 2003 09:04 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.