Oracle 关于去重的就几个方法
刚刚在网上看到了一些同仁写的关于Oracle去重的sql语句看着不错于是就把拌匀一下让大家关注一下
Oracle查询重复数据并删除,只保留一条记录
操作步骤
一、重复记录根据单个字段来判断
1、首先,查找表中多余的重复记录,重复记录是根据单个字段(FIELD_CODE)来判断
select * from table where FIELD_CODE in(select FIELD_CODE from table group by FIELD_CODE having count(FIELD_CODE) >1)
2、删除表中多余的重复记录,重复记录是根据单个字段(FIELD_CODE)来判断,只留有rowid最小的记录
delete from table where (FIELD_CODE) in (select FIELD_CODE from table group by FIELD_CODE having count(FIELD_CODE) >1) and rowid not in (select min(rowid) from table group by FIELD_CODE having count(*)>1)
二、重复记录根据多个字段来判断
1、查找表中多余的重复记录(多个字段)
select * from table a where (a.FIELD_CODE,a.DTA_ITEM_NAME) in(select FIELD_CODE,DTA_ITEM_NAME from table group by FIELD_CODE,DTA_ITEM_NAME having count(*) > 1)
2、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from table a where (a.FIELD_CODE,a.DTA_ITEM_NAME) in (select FIELD_CODE,DTA_ITEM_NAME from table group by FIELD_CODE,DTA_ITEM_NAME having count(*) > 1) and rowid not in (select min(rowid) from table group by FIELD_CODE,DTA_ITEM_NAME having count(*)>1)
3、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from table a where (a.FIELD_CODE,a.DTA_ITEM_NAME) in (select FIELD_CODE,DTA_ITEM_NAME from table group by FIELD_CODE,DTA_ITEM_NAME having count(*) > 1) and rowid not in (select min(rowid) from table group by FIELD_CODE,DTA_ITEM_NAME having count(*)>1)