Duplicate records have two meanings,
The first is a fully duplicated record, that is, all fields are duplicates of the record
Second, some of the key fields are duplicated records, such as the Name field is repeated, and the other fields do not necessarily repeat or repeat can be ignored.
1, for the first kind of repetition, easier to solve, using
SELECT DISTINCT * from TableName
You can get a result set with no duplicate records.
If the table needs to delete duplicate records (duplicate records retain 1), you can delete them as follows
SELECT DISTINCT * to #Tmp from tableName drop table tableName Select * to TableName from #Tmp drop table #Tmp
This duplication occurs because the table is poorly designed and the unique index columns are added to resolve.
2, this kind of repetition problem usually requires to keep the first record in the duplicate record, the operation method is as follows
Suppose there is a duplicate field name,address, which requires the result set to be unique for both fields
Select Identity (int,1,1) as Autoid, * to #Tmp from TableName Select min (autoid) as autoid to #Tmp2 from #Tmp Group by Name SELECT * from #Tmp where autoid in (select Autoid from #tmp2)
The last select is the result set that name,address not duplicate (but one more autoid field, which can be written in the SELECT clause without this column in the actual write)
Distinct and GROUP by remove duplicate field records