删除表中的重复数据

开发过程中准备的测试数据有的时候会出现重复的情况,特别是在数据字典表中的数据,可以通过下面的方法删除重复数据:

 

对于orecle:举例(删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录)

delete from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId ) > 1) 

通过orecle特有的rowid来限定删除的记录

 

对于SqlServer:举例(删除数据字典中的重复数据)

Delete T From

(

select Row_Number() Over(Partition By GroupCode,ItemCode Order By (SELECT 0)) As RowNumber,* From t_gt_Dictionary

) T 

Where T.RowNumber > 1

通过SqlServer特有的Partition by和Row_Number函数来限定删除的记录

 

你可能感兴趣的:(删除)