Solved

How to remove duplicate records in SQL Server table with Primary Key and without primary key?

Posted on 2009-07-13
3
522 Views
Last Modified: 2012-08-14
I found many link to remove duplicate records in SQL Server. But Please let me know which method is the best in SQL Server 2005 and 2008 Table with Primary key and also without primary key. Thanks.
0
Comment
Question by:PKTG
3 Comments
 
LVL 4

Assisted Solution

by:mysteriousguy
mysteriousguy earned 75 total points
ID: 24846474
If the records are really duplicate and have no primary key, i would prefer the possibility described in this blogpost: http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/
If there is a primary key, I would determine the keys of the duplicate rows an delete them by using this key as identifier.
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 100 total points
ID: 24846480
This would be the best method for deleting duplicate records in SQL Server 2005 and 2008:

Hope this helps
-- Table with Primary Key
 

WITH cte(pk, rnum) as

(SELECT pk FROM 

(SELECT pk, row_number() OVER ( partition BY col1, col2 ORDER BY date_col ) rnum

 FROM urtable) temp

WHERE rnum > 1)

DELETE FROM urtable

WHERE pk IN (SELECT pk FROM cte);
 

-- Table without Primary Key but col1 and col2 defines unique records
 

WITH cte(col1, col2, rnum) AS

( SELECT col2 FROM 

(SELECT col1, col2, row_number() OVER ( partition BY col1, col2 ORDER BY date_col ) rnum

 FROM urtable) temp

WHERE rnum > 1)

DELETE FROM urtable

WHERE col1 IN ( SELECT col1 FROM cte)

AND col2 IN ( SELECT col2 FROM cte);

Open in new window

0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 75 total points
ID: 24846539
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now