0

How to Delete Duplicate records from Table in SQL Server ?

Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
Srinivas
  • 329
  • 3
  • 12
  • 32
  • Need more information. Are the record exact duplicates or do you need to merge data (i.e. one record with columns from both rows). Also, you need some sort of key to identity records. – Sparky Mar 02 '14 at 06:31
  • possible duplicate of [How can I remove duplicate rows?](http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows) – Trikaldarshiii Mar 02 '14 at 06:35
  • Provide full information like table schema, duplication type full row duplicate or some fields are duplicate, sql version etc. – Shell Mar 02 '14 at 07:13
  • Full row duplicate..in SQL Server2008 – Srinivas Mar 02 '14 at 07:21

5 Answers5

2
WITH CTE AS(
   SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
       RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
   FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1

Example

Rohit Chaudhari
  • 757
  • 3
  • 14
  • 33
2

To delete rows where the combination of columns col_1, col_2, ... col_n are duplicates, you can use a common table expression;

WITH cte AS (
  SELECT *, 
         ROW_NUMBER() OVER (PARTITION BY col1, ..., col_n ORDER BY col_1) AS rn 
  FROM table_1
)
DELETE FROM cte WHERE rn<>1;

Since the rows are classified by the contents of the listed columns, if the rows are identical in all ways, you'll still need to list all columns in the query.

As always, test and/or backup before running deletes from random people on the Internet on your production data.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

The following questions and the answer given there could be the best help for you

Remove Duplicate Records

You can select the min and max of the rowId (if there is and identity field otherwise add one)

DELETE MyTable 
FROM MyTable
LEFT OUTER JOIN (
   SELECT MIN(RowId) as RowId, Col1, Col2, Col3 
   FROM MyTable 
   GROUP BY Col1, Col2, Col3
) as KeepRows ON
   MyTable.RowId = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL
Use

Source

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

Add an identity column to your table:

Alter table tbl_name add Id int identity(1,1)

Then run following query for deleting records from table:

Delete from tbl_Name where Id not in(select min(Id) from tbl_Name group by RowId)
agold
  • 6,140
  • 9
  • 38
  • 54
Mike Clark
  • 1,860
  • 14
  • 21
0
//duplicate_data_table contains duplicate values 

create temp as (select distinct * from duplicate_data_table);

    drop duplicate_data_table;

create duplicate_data_table as (select * from temp);

    drop temp;
Mark
  • 1
  • 1