Remove duplicate data (repeated exactly the same)

MSsql

Legacy data table a large number of duplicate records, the duplicate data characteristics are: including identification ID, including all data columns are the same, i.e. completely identical repeats. How to remove duplicate records, for example, two identical data, ID, the fields are identical, can not be separated from these two data existing in the data area, respectively, need to be off the field,

Specific steps are as follows:

--1 to add temporary identification field 

1 alter table [表名] 
2 add idd varchar(50)

 

 

--2 update this field, so that data can distinguish

1  update  [ 表名]  set IDD = CHANGE ()

 

--3 remove duplicate, the original identity id by the packet data is greater than 1, taking a minimum deleted

1 delete from [表名] where idd  in(select min(idd) from [表名] group by id having(COUNT(*)>1) )

 

--4 delete temporary increase in column

1  alter  table  [ 表名] 
2  drop  column idd

 

The above is method to delete.

This method only deletes a record number of repetitions is 2, and if there are more duplicate records, can run more than three times.

Note that the backup data before deleting.

 

Guess you like

Origin www.cnblogs.com/jams742003/p/11102390.html