oracle数据库怎么删除重复数据只留一个?
表名是GXS_VIEW_PATIENT_INFO里面有16条完全一样的数据没有主键,删除其他15条数据,只留下一条语句!大神们帮忙...
表名是 GXS_VIEW_PATIENT_INFO 里面有16条完全一样的数据没有主键,删除其他15条数据,只留下一条语句!大神们帮忙
展开
4个回答
推荐于2017-09-24
展开全部
查询及删除重复记录的SQL语句
1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 ;
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录;
DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);
3、查找表中多余的重复记录(多个字段);
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录;
delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录;
select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 ;
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录;
DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);
3、查找表中多余的重复记录(多个字段);
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录;
delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录;
select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
展开全部
delete from GXS_VIEW_PATIENT_INFO where rowid not in (select min(rowid) from GXS_VIEW_PATIENT_INFO group by 某字段)
某字段就是按照某几个分组
比如数据
id name
1 a
1 a
1 a
你最后group by id或者group by name都可以
但是如果
id name
1 a
1 a
1 a
2 a
但是像上边这样,最后也只能保留一条,所以你得找到一个group by 的字段
不过这个慎用,数据多的时候会很慢的,删除前备份一下数据吧
更多追问追答
追问
运行失败!ROWID是指随意的一个id吧 某字段可以与rowid是一个字段吧!
追答
不可以
rowid是一个虚拟列,是oracle中的一个数据的物理地址
你只需要换“某字段”换成某个字段就行
要是就像你说的,16条重复,你只保留一条
delete from GXS_VIEW_PATIENT_INFO where rowid not in (select min(rowid) from GXS_VIEW_PATIENT_INFO)
那就直接这样,不过一定要备份哈
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-02
展开全部
DELETE FROM GXS_VIEW_PATIENT_INFOWHERE rowid not in(SELECT MIN(rowid)FROM GXS_VIEW_PATIENT_INFOGROUP BY column1, column2, column3);
其中column1,column2,column3组合能唯一确定数据
其中column1,column2,column3组合能唯一确定数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询