查重的sql oracle,SQL表内查重和删重

本文介绍了如何在SQL中查询和删除重复记录。提供了两种查询重复记录的方法,一种基于单个列,另一种基于多个列的组合。同时,针对PostgreSQL和Oracle数据库,给出了删除重复记录的SQL语句,保留每组重复记录中的一条。这些技巧对于数据清洗和数据库维护至关重要。
摘要由CSDN通过智能技术生成

###查询重复记录

select * from my_tab

where dup_col in(select dup_col from my_tab

group by dup_col having count(dup_col)>1)

order by dup_col;

###查询重复记录(多个条件)

select * from my_tab as t1

where

(

select count(*) from my_tab t2

where

t2.col_1=t1.col_1 and

t2.col_2=t1.col_2 and

t2.col_3=t1.col_3 and

t2.col_4=t1.col_4

)>1;

###删除重复记录(只留一条)

PG:使用ctid

delete from my_tab

where dup_col in (select dup_col from my_tab group by dup_col having count(dup_col) > 1)

and ctid not in (select min(ctid) from my_tab group by dup_col having count(dup_col)>1);

delete from table_1

where (col_1,col_2,col_3) in

(select col_1,col_2,col_3 from table_1

group by col_1,col_2,col_3

having count(*) > 1)

and ctid not in

(select min(ctid) from table_1

group by col_1,col_2,col_3

having count(*)>1);

ORACLE:使用rowid

delete from my_tab

where dup_col in (select dup_col from my_tab group by dup_col having count(dup_col) > 1)

and rowid not in (select min(rowid) from my_tab group by dup_col having count(dup_col)>1);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值