Database Simplified Headline Animator

Database Simplified

Thursday 9 June 2011

How To Remove Duplicate Records From A Table

 

Consider Following Table For Example Which Contains Duplicate Country Names :

Select * From Country_Master

Go

image

 

Let’s See How Many Duplicate Records Each Country Contains.

Select CountryName,COUNT(CountryName) as NoOfRecords from Country_Master
Group By CountryName
Go

image

 

Now You Can Use Following Query to Remove Duplicate Rows

Declare @CountryName Varchar(100)
Declare @Records Int
Declare DelDuplicate Cursor 

For
Select CountryName,COUNT(CountryName)-1 as NoOfRecords  --Get Count Of (Records Country Wise)-1
from Country_Master
Group By CountryName
Having Count(CountryName)>1 -- Include Only Those Records which are More then 1

Open DelDuplicate
Fetch DelDuplicate Into @CountryName,@Records
While @@FETCH_STATUS=0
    Begin
        Set RowCount @Records -- Set RowCount (Total Record Country Wise) - 1
            Delete From Country_Master Where CountryName=@CountryName -- Delete Duplicate Records
        Set RowCount 0 -- Reset Record To Default
        Fetch DelDuplicate Into @CountryName,@Records
    End 
Close DelDuplicate 
DeAllocate DelDuplicate

image

 

As You Can See the Major Role Here Is Played By RowCount

For Example When I Set RowCount = 1 and you run Select Command On table Then Irrespective of No Of Records In Table. Result will Show only 1 Row and Even  if You Delete From Table only 1 row Will Be Deleted.

Default Value Of RowCount is 0.

Which Returns All The Rows Of Table in Select Statement.

 

Now Let’s See What happened to the Table containing Duplicate Country Names

image

 

Open-mouthed smile

1 comment:

  1. Hi,

    I have a question, is it better to use cursor to remove duplicates or by using CTE as below

    WITH CTE(CountryName, CountryIndex)
    AS
    (SELECT CountryName, ROW_NUMBER() OVER(PARTITION BY CountryName ORDER BY CountryName) AS CountryIndex FROM COUNTRY_MASTER)


    DELETE FROM CTE WHERE CountryIndex> 1

    SELECT * FROM COUNTRY_MASTER

    ReplyDelete