Monday, 16 December 2013

Find and Delete Duplicate records (Ambiguity) in database while keeping one (In SQL Server)

Find and Delete Duplicate records (Ambiguity) in database while keeping one (In SQL Server)

In our daily life with SQL, we can have a situation where we haven't applied a primary/ unique key on our database table or we do have a primary/ unique key on a column however reset of the columns can have duplicate information (Ambiguity) or our logic for uniquely identifying a record fails  .
In such situation we can have multiple records with same data. Now this is what we call a problem. We need to identify those duplicate records and delete them while keeping one or we can say the original one.
We can do this with the help of ROW_NUMBER() function in SQL Server.

Here is the solution 

Note:- I am explaining it from the very beginning i.e. from creating a table to inserting duplicate records to deleting them (for beginners), you can directly scroll down for the actual query if you are not a beginner.
So lets practice and start with the beginning.

Step 1

Use/ Create a database

Query
CREATE DATABASE SQLisAwesome

USE SQLisAwesome

Step 2

Create a Table

Query
 CREATE TABLE Dummy (
    [Id] [INT]  ,
    [Name] [VARCHAR](100) NOT NULL,
    [Designation] [VARCHAR](100) NOT NULL,
    [Salary] [INT] NOT NULL
)

Step 3

Insert dummy values

Query
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ram','SE',25000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)

Step 4

Now check the data

Query
SELECT * FROM [Dummy]
All Records

Identifying the duplicate records

Query
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
Records with Duplicate Record Number equal to the occurrence of that particular record

Now you can identify the duplicate records (duplicate for given columns written after the keywords "PARTITION BY" in above query i.e. "Id, Name, Designation, Salary") with the help of DuplicateRowNo columns
as DuplicateRowNo this will have value 1 if record is not duplicate and more than 1 (2,3,4) if record is duplicate according to the occurrence of that particular record (Number of time the record is repeating it self).

Now separating the duplicate record form the original one(Keep one select rest)
we can do this using Common Table Expressions (CTE)

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) SELECT * FROM ABC WHERE DuplicateRowNo>1
Duplicate record having occurrence more than one (Keeping original one)

Note:- use semicolon (;) in before WITH keyword if any query is written before it.

Step 5

Delete the Duplicate

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) DELETE FROM ABC WHERE DuplicateRowNo>1

In Addition
Now suppose you have a Identity column in table which is unique for all records but rest of the columns have duplicate values

Step 6 

Query
ALTER TABLE [Dummy] DROP COLUMN Id
LTER TABLE [Dummy] ADD Id INT NOT NULL UNIQUE IDENTITY(1,1)

Now you will have a Unique, Auto Increment column which cannot have duplicate value however rest of the columns can have.
Insert Dummy values, Leave column Id as it is now Auto Increment.

Query
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ram','SE',25000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)

Step 7

Now check the data

Query
SELECT * FROM [Dummy]
Duplicate records with unique identity key (Id)
Note:- SQL Server will automatically insert integer numbering in Id column for already existing records.

Identifying the duplicate records. This time do not write Column [Id] After "PARTITION BY" keywords

Query
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]

Sparating the duplicate record form the original one(Keep one select rest) using CTE

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) SELECT * FROM ABC WHERE DuplicateRowNo>1

Step 8

Delete the Duplicate

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) DELETE FROM ABC WHERE DuplicateRowNo>1

Done
Check Your data

Query
SELECT * FROM [Dummy]

No comments:

Post a Comment