網(wǎng)站首頁 編程語言 正文
Oracle刪除重復(fù)數(shù)據(jù)只保留一條
查詢及刪除重復(fù)記錄的SQL語句
1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(Id)來判斷
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(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、查找表中多余的重復(fù)記錄(多個(gè)字段)
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有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、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含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)
Oracle刪除重復(fù)記錄,保留一條,沒有主鍵的情況
想偷懶,網(wǎng)上搜一個(gè),結(jié)果沒有找到合適的,自己寫個(gè)吧。
有主鍵的比較簡單,網(wǎng)上也很多。
--id為主鍵 a是有重復(fù)值的字段
begin
? for v in (select a, min(id) id, count(*)
? ? ? ? ? ? ? from temp_a
? ? ? ? ? ? ?group by a
? ? ? ? ? ? having count(*) > 1) loop
? ? delete from temp_a t
? ? ?where t.a = v.a
? ? ? ?and t.id <> v.id;
? ? commit;
? end loop;
end;
沒有主鍵的話,可以用的通過rowid可以實(shí)現(xiàn)。這個(gè)網(wǎng)上也很多。思路與主鍵id一樣
--a是有重復(fù)值的字段
begin
? for v in (select a, min(rowid) id, count(*)
? ? ? ? ? ? ? from temp_a
? ? ? ? ? ? ?group by a
? ? ? ? ? ? having count(*) > 1) loop
? ? delete from temp_a t
? ? ?where t.a = v.a
? ? ? ?and t.rowid <> v.id;
? ? commit;
? end loop;
end;
剛開始是想通過rownum實(shí)現(xiàn)的,發(fā)現(xiàn)會(huì)有問題,比如:
--a是有重復(fù)值的字段,這個(gè)sql不會(huì)刪除任何數(shù)據(jù)
begin
? for v in (select a, count(*) from temp_a group by a having count(*) > 1) loop
? ? delete from temp_a t
? ? ?where t.a = v.a
? ? ? ?and rownum <> 1;
? ? commit;
? end loop;
end;
這個(gè)是刪不了數(shù)據(jù)的,因?yàn)閞ownum總是從1開始的。第一行不符合的話,第二行的rownum又會(huì)成為1。在temp_a表有數(shù)據(jù)的情況下,下邊這個(gè)sql查不到任何數(shù)據(jù),改成>10也是一樣的。而<10可以查到前9條數(shù)據(jù)。
select * from temp_a where rownum>1;
如果一定想用rownum的話,還有一種做法,就是增加臨時(shí)列,值等于rownum,這樣就相當(dāng)于有了主鍵了。
--新增v_id=rownum作為臨時(shí)主鍵 a是有重復(fù)值的字段
alter table temp_a add v_id number(10);
update temp_a t set t.v_id = rownum;
commit;
begin
? for v in (select a, min(v_id) v_id, count(*)
? ? ? ? ? ? ? from temp_a
? ? ? ? ? ? ?group by a
? ? ? ? ? ? having count(*) > 1) loop
? ? delete from temp_a t
? ? ?where t.a = v.a
? ? ? ?and t.v_id <> v.v_id;
? ? commit;
? end loop;
end;
alter table temp_a drop column v_id;
總結(jié)
原文鏈接:https://blog.csdn.net/qq_39997939/article/details/122893262
相關(guān)推薦
- 2024-07-18 Spring Security之認(rèn)證過濾器
- 2023-05-23 Numpy數(shù)組轉(zhuǎn)置的實(shí)現(xiàn)_python
- 2023-01-15 C++實(shí)現(xiàn)自定義撤銷重做功能的示例代碼_C 語言
- 2022-10-31 Python3邏輯運(yùn)算符與成員運(yùn)算符_python
- 2022-11-23 Python面向?qū)ο蟮膬?nèi)置方法梳理講解_python
- 2022-07-30 Linux文件管理命令行
- 2022-05-28 C語言數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解單向鏈表_C 語言
- 2023-11-22 fatal: unable to access ‘https://github.com/xxxxx/
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支