Solved

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

Posted on 2009-07-13
3
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

718 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