27.3.10

Delete duplicate records in SQL Server

==========================================================================================
/* TABLE WITH MULTIPLE COLUMNS*/
==========================================================================================
Considering this table is a stand alone table with multiple columns and there is no identity.
Select the distict values into temp table, drop the existing table and rename the temp table into the original table.
==========================================================================================
drop table venkat
create table venkat(id int ,nam varchar(100))
insert into venkat values (1,'Arun')
insert into venkat values (1,'Arun')
insert into venkat values (1,'Arun')
insert into venkat values (2,'Arun1')
insert into venkat values (2,'Arun1')
select * from venkat
-- GET DISTINCT RECORDS INTO TEMP TABLE.
SELECT DISTINCT * INTO temp_Venkattable FROM venkat
-- DROP EXISTING TABLE.
DROP TABLE venkat
-- RENAME THE NEW TABLE TO EXISTING TABLE.
EXEC sp_rename 'temp_Venkattable','venkat'
select * from venkat
-- IF THE TABLE IS NOT A STAND ALONE TABLE, YOU CAN'T DO THE ABOVE PROCESS. BEFORE ACHIEVING IT,
--YOU NEED TO CONSIDER ALL THE DEPENDENCIES ON THE TABLE
==========================================================================================
/* TABLE WITH IDENTITY COLUMN*/
==========================================================================================

Considering this table having an identity column. In that case, our work is too simple.
==========================================================================================

drop table venkat
create table venkat(id int IDENTITY ,nam varchar(100))
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun1')
insert into venkat values ('Arun1')
select * from venkat
DELETE FROM VENKAT WHERE ID NOT IN
(SELECT MIN(ID) FROM VENKAT GROUP BY NAM)
select * from venkat
==========================================================================================
/* TABLE WITH SINGLE COLUMNS*/
==========================================================================================
drop table venkat
create table venkat(nam varchar(100))
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun1')
insert into venkat values ('Arun1')
select * from venkat
-- CTE IS THE VERY BEST OPTION TO DELETE THE DUPLICATE RECORDS IN SQL SERVER 2005
with cte as (
select row_number() over ( partition by nam order by nam ) as id ,nam from venkat
)
delete from cte where id !=1
==========================================================================================

2 comments:

  1. Thank you for sharing your knowledge, the post I looked up was most helpful.

    ReplyDelete
  2. Hi Venkat,

    The query seems to be complicated. Why don't you post a simple query used to delete a duplicate row in a table. Freshers and college goers may not able to understand the query.

    ReplyDelete