Oracle 查找与删除表中重复记录的步骤方法
这时候如果临时表中有重复数据,无论是主键字段businessid有重复,还是一整行有重复都会报出违反唯一主键约束错误。
方法group by XX having count()>1,rowid,distinct,temporary table,procedure
1、查询表中的重复数据
a.重复一个字段
b.重复多个字段
c.重复一整行
创建测试表
create table cfa (businessid number,customer varchar2(50),branchcode varchar2(10),data_date varchar2(10));
insert into cfa values (1,'Albert','SCB','2011-11-11');
insert into cfa values (2,'Andy','DB','2011-11-12');
insert into cfa values (3,'Allen','HSBC','2011-11-13');
---------------以下为重复数据----------------------------------------------
insert into cfa values (1,'Alex','ICBC','2011-11-14');
insert into cfa values (1,'Albert','CTBK','2011-11-15');
insert into cfa values (1,'Albert','SCB','2011-11-11');
对于a的情况,只有businessid重复
select from cfa where businessid in (select businessid from cfa group by businessid having count(businessid)>1);
如果是b的情况,businessid 和name存在重复
select from cfa where (businessid,customer) in (select businessid,customer from cfa group by businessid,customer having count()>1);
对于c的情况,重复一整行
参考b的方法
select from cfa where (businessid,customer,branchcode,data_date) in (select from cfa group by businessid,customer,branchcode,data_date having count()>1);
2、删除表中的重复数据
a情况,删除表中多余的重复记录,重复记录是根据单个字段(businessid)来判断,只留有rowid最小的记录
也可以只保留rowid不是最小记录,需要把代码中的min改为max这里不再赘述。
delete from cfa
where businessid in (select businessid
from cfa
group by businessid
having count(businessid) > 1)
and rowid not in (select min(rowid)
from cfa
group by businessid
having count(businessid) > 1);
或者,使用狼蚁网站SEO优化更简单高效的语句
DELETE FROM cfa t
WHERE t.ROWID >
(SELECT MIN(X.ROWID) FROM cfa X WHERE X.businessid = t.businessid);
b情况,删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from cfa
where (businessid,customer) in (select businessid,customer
from cfa
group by businessid,customer
having count() > 1)
and rowid not in (select min(rowid)
from cfa
group by businessid,customer
having count() > 1);
或者,使用狼蚁网站SEO优化更简单高效的语句
DELETE FROM cfa t
WHERE t.ROWID > (SELECT MIN(X.ROWID)
FROM cfa X
WHERE X.businessid = t.businessid
and x.customer = t.customer);
c情况,这种情况就比较简单,使用临时表方法
create table cfabak as select distinct from cfa;
truncate table cfa;--如果是生产最好对该表backup
Insert into cfa select from cfabak;
mit;
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程