首页 > 技术文章 > oracle-删除多余重复数据

seeall 2020-10-07 15:04 原文

时间:2020年10月7日

前言概述:

  以下适合数据量比较少的情况下;

  且如果多条记录的同一个字段为空,无法删除;即比较两条记录是否相同,选取的字段最好不为空;

 

-- 查找表中多余的重复的记录,不包含rowid最小的记录

select * from 表名 a
where (a.字段1, a.字段2, a.字段3)
in(select 字段1, 字段2, 字段3 from 表名
  group by 字段1, 字段2, 字段3
  having count(*) > 1)
and rowid not in(select min(rowid) from 表名
  group by 字段1, 字段2, 字段3
  having count(*) > 1)
order by a.字段1


-- 删除表中多余的重复的记录,不包含rowid最小的记录
delete from 表名 a
where (a.字段1, a.字段2, a.字段3)
in(select 字段1, 字段2, 字段3 from 表名
  group by 字段1, 字段2, 字段3
  having count(*) > 1)
and rowid not in(select min(rowid) from 表名
  group by 字段1, 字段2, 字段3
  having count(*) > 1)

having的作用:类似于起到一个筛选的作用,比如筛选数量(count)多于多少的、字段之和(sum)大于多少的

推荐阅读