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 2000 Forums
 Transact-SQL (2000)
 DELETE duplicate rows

Author  Topic 

jamonakes
Starting Member

18 Posts

Posted - 2006-03-27 : 10:44:28
Someone recently re-imported records that were already in my table to the same table and created duplicate, triplicate and quadruplicate records. The relevant columns in the table look like this:
detCellNo detMsg detDateCommitted
--------- ------ ----------------

The only way I can know a record is a repeat is by detDateCommitted which is the surest because it is an exact match for any repeated records. I want to delete quadruplicates, truplicates and duplicates such that I remain with only record one of each. How can I achieve that?

This is what I tried to do. for the quadruplicates, I did
DELETE tblMyTable from (SELECT top 3 detCellNo, detMsg, detDateCommitted from tblMyTable WHERE detOfImport >= '[HERE I PASS THE DATE THE IMPORT WAS DONE, NOT SURE IF ITS NECESSARY]' GROUP BY detCellNo, detMsg, detDateCommitted having count=4) as t1 Where tblMytable.detDateCommitted = t1.detDateCommitted

I was to repeat this for triplicates, changing top 3 to top 2 and count=4 to count=3. and the same for duplicates. It didnt work.
Can someone show me the most effecient way of doing this?

X002548
Not Just a Number

15586 Posts

Posted - 2006-03-27 : 11:38:26
Why don't you post the table DDL and read my hint link in my sig.

First things first though...you need to identify the population.

And it sounds like there is no PK on the table,

Bu try

SELECT Col list FROM myTable99 GROUP BY Col list HAVING COUNT(*) > 1



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-03-27 : 11:55:22
Select F1, F2 into T4_NoDup from T4 Group by F1, F2

http://support.microsoft.com/default.aspx?scid=kb;en-us;139444

and

http://www.sqlteam.com/item.asp?ItemID=3331

Also read about UNION in BOL
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-03-27 : 12:16:03
quote:
Originally posted by Srinika

Select F1, F2 into T4_NoDup from T4 Group by F1, F2

http://support.microsoft.com/default.aspx?scid=kb;en-us;139444

and

http://www.sqlteam.com/item.asp?ItemID=3331

Also read about UNION in BOL



Looks like an Access fan

SELECT DISTINCT F1, F2 INTO....

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-03-27 : 12:58:43
quote:
Originally posted by X002548
Looks like an Access fan
SELECT DISTINCT F1, F2 INTO....



A Programmer converted to a DB guy - Now Hybrid "Pro-Base" [Programmer-DataBase]
So some Access, Some VB, Some .net, Some SQL Server,
What to do?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-28 : 01:50:37
Also refer this
http://sqlteam.com/forums/topic.asp?TOPIC_ID=6256

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -