Removing Duplicates using CTE in Sql Server

No.of Views1269
Bookmarked0 times
Downloads 
Votes0
By  Mohammad Shahanshah Ansari   On  18 Jun 2010 11:06:28
Tag : Sql Servers , T-SQL
Removing Duplicates using CTE in Sql Server
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Here's a code snippet which removes the duplicate records,using CTE.Most of the time we are face problems  with duplicate data in database. based on the database design you have to write code to delete duplicate records from table.

To delete duplicate records , there are so many ways.

1.direct query

2.using stored proc

3.CTE.

In this code snippet give a common way of delete duplicates records using CTE.

CodeSnippet

WITH CTE ([col1], cnt)AS(SELECT [col1],ROW_NUMBER() OVER(PARTITION BY [col1] ORDER BY [col1]) AS cnt
FROM table1
)DELETEFROM CTE
WHERE cnt > 1

Copy above script and paste in your database and change col1 as it your table column name.that's all i hope this is help to you all.

 
Sign Up to vote for this article
 
About Author
 
Mohammad Shahanshah Ansari
Occupation-Software Engineer
Company-Utopia Inc.
Member Type-Fresh
Location-India
Joined date-16 Jun 2010
Home Page-
Blog Page-http://think-sql.blogspot.com
A software engineer with more than 4 Years of experience in different technologies which includes .Net, Java, Sql Server and SAP business objects. Currently working for one of the biggest utility company in Kingdom of Saudi Arabia as a Data Migration Specialist on a SAP Implementation project.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top