Saturday, May 19, 2012

Delete Duplicate Rows From Table....


In this Article I will explain you about how to delete Duplicate rows from Table

Our Table does not contain any primary key column because of that it contains duplicate records that would be like this

Now just Run this Querry 

WITH tempTable as
(
select ROW_NUMBER() Over(partition by Name,Class order by Name) As RowNumber,* from ClassData
)
select * from temptable

OutPut Of this Querry will be 
If you observe above table I added another column RowNumber this column is used to know which record contains duplicate values based on rows with RowNumber greater than 1.  

Now we want to get the records which contains unique value from datatable for that we need to write the query like this


WITH tempTable as
(
select ROW_NUMBER() Over(partition by Name,Class order by Name) As RowNumber,* from ClassData
)
delete from temptable where RowNumber>1
select * from ClassData;

When you Run this Querry OutPut will be like this......


0 comments:

Post a Comment