oracle某个字段有重复数据,如何删除多余数据只保留1条
例如表Z有ID、NAME、DIANHUA、DIZHI....等多项,其中ID有重复的数据,现想将ID重复数据删除,只保留1条,通过什么语句。网上查询用deletefrom...
例如表 Z 有 ID、NAME、DIANHUA、DIZHI .... 等多项,其中ID有重复的数据,现想将ID重复数据删除,只保留1条,通过什么语句。
网上查询用delete from z a where id in ( select id ,count(*) from z group by id having count(*) >1) 报值过多。
请高手给一个删除方法,谢谢! 展开
网上查询用delete from z a where id in ( select id ,count(*) from z group by id having count(*) >1) 报值过多。
请高手给一个删除方法,谢谢! 展开
5个回答
2015-05-08
展开全部
Oracle 删除重复数据只留一条
查询及删除重复记录的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)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-02-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
oracle中在一张表中某个字段下面有重复记录,有很多方法,但是有一个方法,是比较"高效"的,如下语句:
SELECT data_guid
FROM adam_entity_datas a
WHERE a.rowid > (SELECT MIN(b.rowid)
FROM adam_entity_datas b
WHERE b.data_guid = a.data_guid)
如果表中有大量数据,但是"重复数据比较少",那么可以用下面的语句提高效率
SELECT data_guid
FROM adam_entity_datas
WHERE data_guid IN (SELECT data_guid
FROM adam_entity_datas
GROUP BY data_guid
HAVING COUNT(*) > 1)
此方法查询出所有重复记录了,也就是说,只要是重复的就选出来,下面的语句也许"更高效"
SELECT data_guid
FROM adam_entity_datas
WHERE ROWID IN (SELECT rid
FROM (SELECT ROWID rid,
row_number() over(PARTITION BY data_guid ORDER BY ROWID) m
FROM adam_entity_datas)
WHERE m <> 1)
SELECT data_guid
FROM adam_entity_datas a
WHERE a.rowid > (SELECT MIN(b.rowid)
FROM adam_entity_datas b
WHERE b.data_guid = a.data_guid)
如果表中有大量数据,但是"重复数据比较少",那么可以用下面的语句提高效率
SELECT data_guid
FROM adam_entity_datas
WHERE data_guid IN (SELECT data_guid
FROM adam_entity_datas
GROUP BY data_guid
HAVING COUNT(*) > 1)
此方法查询出所有重复记录了,也就是说,只要是重复的就选出来,下面的语句也许"更高效"
SELECT data_guid
FROM adam_entity_datas
WHERE ROWID IN (SELECT rid
FROM (SELECT ROWID rid,
row_number() over(PARTITION BY data_guid ORDER BY ROWID) m
FROM adam_entity_datas)
WHERE m <> 1)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-05-08
展开全部
不能直接选出来删,直接delete会把所有重复的ID全部干掉
create table xxx as
select distinct ID、NAME、DIANHUA、DIZHI from z
create table xxx as
select distinct ID、NAME、DIANHUA、DIZHI from z
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
delete from z t where t.rowid not in (select min(rowid) from z t1 where t1.id=t.id group by id)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询