網站首頁 編程語言 正文
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)
Oracle刪除重復記錄,保留一條,沒有主鍵的情況
想偷懶,網上搜一個,結果沒有找到合適的,自己寫個吧。
有主鍵的比較簡單,網上也很多。
--id為主鍵 a是有重復值的字段
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可以實現。這個網上也很多。思路與主鍵id一樣
--a是有重復值的字段
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實現的,發現會有問題,比如:
--a是有重復值的字段,這個sql不會刪除任何數據
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;
這個是刪不了數據的,因為rownum總是從1開始的。第一行不符合的話,第二行的rownum又會成為1。在temp_a表有數據的情況下,下邊這個sql查不到任何數據,改成>10也是一樣的。而<10可以查到前9條數據。
select * from temp_a where rownum>1;
如果一定想用rownum的話,還有一種做法,就是增加臨時列,值等于rownum,這樣就相當于有了主鍵了。
--新增v_id=rownum作為臨時主鍵 a是有重復值的字段
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;
總結
原文鏈接:https://blog.csdn.net/qq_39997939/article/details/122893262
相關推薦
- 2022-06-07 SQL?Server內存機制詳解_MsSql
- 2022-04-19 盤點分析C語言中少見卻強大的字符串函數_C 語言
- 2022-12-22 Android開發input問題解決分析_Android
- 2022-11-29 C#中泛型類和擴展方法如何使用_C#教程
- 2022-11-22 GoLang?channel使用介紹_Golang
- 2022-07-02 一個Python優雅的數據分塊方法詳解_python
- 2022-09-22 element form表單循環校驗(動態綁定的數據)
- 2022-11-22 Android?RecyclerView使用入門介紹_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支