Oracle、SQLServer 删除表中的重复数据,只保留一条记录

原文地址:

https://blog.csdn.net/yangwenxue_admin/article/details/51742426

https://www.cnblogs.com/springsnow/p/10334469.html

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

****************Oracle 删除表中的重复数据****************

一、重复记录根据单个字段来判断

1、首先,查找表中多余的重复记录,重复记录是根据单个字段(FIELD_CODE)来判断

select * from R_RESOURCE_DETAILS where FIELD_CODE in
(select FIELD_CODE from R_RESOURCE_DETAILS group by FIELD_CODE having count(FIELD_CODE) >)

2、删除表中多余的重复记录,重复记录是根据单个字段(FIELD_CODE)来判断,只留有rowid最小的记录

delete from R_RESOURCE_DETAILS where (FIELD_CODE) in
(select FIELD_CODE from R_RESOURCE_DETAILS group by FIELD_CODE having count(FIELD_CODE) >)
and rowid not in (select min(rowid) from R_RESOURCE_DETAILS group by FIELD_CODE having count(*)>)

二、重复记录根据多个字段来判断

1、查找表中多余的重复记录(多个字段)

select * from R_RESOURCE_DETAILS a where (a.FIELD_CODE,a.DTA_ITEM_NAME) in
(select FIELD_CODE,DTA_ITEM_NAME from R_RESOURCE_DETAILS group by FIELD_CODE,DTA_ITEM_NAME having count(*) > )

2、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from R_RESOURCE_DETAILS a where (a.FIELD_CODE,a.DTA_ITEM_NAME) in
(select FIELD_CODE,DTA_ITEM_NAME from R_RESOURCE_DETAILS group by FIELD_CODE,DTA_ITEM_NAME having count(*) > )
and
rowid not in (select min(rowid) from R_RESOURCE_DETAILS group by FIELD_CODE,DTA_ITEM_NAME having count(*)>)

3、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from R_RESOURCE_DETAILS a
where (a.FIELD_CODE,a.DTA_ITEM_NAME) in
(select FIELD_CODE,DTA_ITEM_NAME from R_RESOURCE_DETAILS group by FIELD_CODE,DTA_ITEM_NAME having count(*) > )
and
rowid not in (select min(rowid) from R_RESOURCE_DETAILS group by FIELD_CODE,DTA_ITEM_NAME having count(*)>)

****************SQLServer 删除表中的重复数据****************

1、查询表中Name 重复的数据
2、有唯一列,通过唯一列最大或最小方式删除重复记录
3、无唯一列使用ROW_NUMBER()函数删除重复记录

1、查询表中Name 重复的数据

select Name from Student group by  Name having count(Name) > 1

2、有唯一列,通过唯一列最大或最小方式删除重复记录

检查表中是否有主键或者唯一值的列,当前可以数据看到ID是唯一的,可以通过Name分组排除掉ID最大或最小的行

delete from Student
where Name in( select Name from Student group by Name having count(Name) > 1) and
ID not in(select max(ID) from Student group by Name having count(Name) > 1 )

执行删除脚本后查询

Oracle、SQLServer 删除表中的重复数据,只保留一条记录

3、无唯一列使用ROW_NUMBER()函数删除重复记录

如果表中没有唯一值的列,可以通过 来删除重复数据 
重复执行插入脚本,查看表数据,表中没有唯一列值

Oracle、SQLServer 删除表中的重复数据,只保留一条记录

Delete T From
(Select Row_Number() Over(Partition By [Name] order By [ID]) As RowNumber,* From Student)T
Where T.RowNumber > 1

小知识点

语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
         表示根据COLUMN分组,在分组内部根据 COLUMN排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)
         函数“Row_Number”必须有 OVER 子句。OVER 子句必须有包含 ORDER BY
         Row_Number() Over(Partition By [Name] order By [ID])  表示已name列分组,在每组内以ID列进行升序排序,每组内返回一个唯一的序号

执行删除脚本后查询表数据

Oracle、SQLServer 删除表中的重复数据,只保留一条记录

上一篇:Golang 在 Mac、Linux、Windows 下如何交叉编译(转)


下一篇:[示例] Firemonkey 图片按钮(3态)