Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How can I purge rows that match a duplicate field?

Author  Topic 

johnvm
Starting Member

18 Posts

Posted - 2007-08-31 : 15:32:35
Hey guys, I've been reading up on other threads about how to purge duplicate rows, etc., but my situation is a little different from what I've found so far so I figured I'd make a new post.

I'm trying to set a field to be the master key of a table, but there are duplicate entries. Select DISTINCT doesnt work, because for instance, these might be two rows (fields separated by commas):

KEYCOLUMN,COLUMN2,COLUMN3
key1, abcde, abcde
key1, abcdef, abcdef

So the two rows are distinct from eachother, but the key column in each is not. I need a way to delete all the rows where the key column is not distinct.... so then i can set that key column as the primary key. How can I go about doing this? Thanks!

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-08-31 : 16:34:31
How do you know which row to keep?
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-08-31 : 16:34:59
do a group by on the key column and query for count(*) > 1. this will give you all dup records.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

johnvm
Starting Member

18 Posts

Posted - 2007-09-01 : 02:00:21
i don't care which record i keep. this isn't critical data, i just need the duplicates gone. to be honest i could even delete both records in an instance of a duplicate and be happy.

will fiddle to see if i can get that dupe by method working -- i'm a newb at sql though so might have limited success. examples would be appreciated.

thx for the help given already and any more coming!
Go to Top of Page

johnvm
Starting Member

18 Posts

Posted - 2007-09-01 : 02:12:24
SELECT id, count(*)
FROM table
GROUP BY id
HAVING count(*) > 1

seems to work. thanks guys.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-01 : 04:06:55
DELETE f
FROM (SELECT Col1, ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY Col1) AS RecID FROM Table1) AS f
WHERE RecID > 1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -