Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 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 December 18th, 2006, 01:58 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to delete unique rows but with duplicate colum

Hello all.

I'm desperate here.

I Have a table with 5 columns.

CREATE TABLE MyTable
(M_ID smallint,
A_ID smallint,
AW char(10),
ST_O char(10),
MC char(300) )

I didn't set any primary key at the beginning, so i could import data from a txt file through the SQL import and Export Wizard(3 million records). This txt contains many duplicate records (rows are same in all columns) but it contains also duplicate records in M_ID and A_ID fields. I want to make these fields PK.

I already cleaned up the table from the rows that are exactly the same (with only one instance left - i used the method with the temp table), but i cannot delete the rows wich contains the duplicate values in the two first columns, and leave only one of them. I don't care which of the rows, i just want the duplicates to be deleted, so there is only one compination M_ID and A_ID.

for instance...

 M_ID A_ID AW ST_O MC
====== ====== ====== ====== ======

123 981 FIR REL MI
123 981 SEC RELTG PS
256 315 ALO TRM PP
256 315 POW DTM KL
473 787 LKJ ERT IO
473 787 JKM WSA IK

i want to have only

 M_ID A_ID AW ST_O MC
====== ====== ====== ====== ======

123 981 FIR REL MI
256 315 ALO TRM PP
473 787 LKJ ERT IO

Hope i made my self clear. :)

All this is for a project for my university.(i must have it ready till tomorrow)

Thanks in advance!

 
Old December 18th, 2006, 03:05 PM
Authorized User
 
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is a script to review. Let me know if this works for you...

-- Add IDENTITY column
ALTER TABLE dbo.MyTable ADD MyTableID int IDENTITY(1,1) NOT NULL;

-- Remove Duplicates
DELETE a
FROM dbo.MyTable a
JOIN(SELECT MIN(MyTableID) maxid, M_ID, A_ID FROM dbo.MyTable GROUP BY M_ID, A_ID) b ON a.M_ID = b.M_ID AND a.A_ID = b.A_ID
WHERE a.MyTableID != b.maxid;

-- Drop IDENTITY Column
ALTER TABLE dbo.MyTable DROP COLUMN MyTableID;

-- Add CONSTRAINT
ALTER TABLE ADD CONSTRAINT PK_MyTable PRIMARY KEY NONCLUSTERED (M_ID, A_ID);

Adam Gossage
Lake Wylie, SC, USA
 
Old December 18th, 2006, 04:38 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

worked perfectly!

i don't have words to thank you!

so thank you! :)

 
Old January 3rd, 2007, 04:57 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

only issue with this solution is if your doing the alter table while the table is in heavy use transactions will likely be rejected by the server. I like using cursors but it's not needed if your not in a high volume, in production situation with that table.






Similar Threads
Thread Thread Starter Forum Replies Last Post
How can you delete duplicate rows without using te subhasischakraborty SQL Server 2000 15 October 19th, 2007 06:51 AM
duplicate rows msrnivas Classic ASP Components 1 August 26th, 2004 02:07 AM
Detecting duplicate rows. Daniel Walker PHP Databases 6 June 21st, 2004 02:39 AM
Checking Duplicate values and delete rows ppenn Excel VBA 2 February 3rd, 2004 06:57 AM
duplicate nulls in a column with unique constraint defiant SQL Server 2000 3 October 22nd, 2003 01:58 AM





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