oracle删除重复数据并只保留一条数据

1.delete from t_test t1 where t1.id in (select t.id from t_test t group by t.id having count(1)>1)

and t1.rowid not in (select min(rowid) from t_test t group by t.id having count(*)>1)


补充:having和where区别

1.where:约束声明,在返回结果产生作用不支持聚合函数。

2.having:过滤声明,在返回结果后产生作用,从结果集中进行过滤,支持聚合函数。

3.聚合函数:sum、avg、min、max、count等函数


2.使用中间表

create table t_test1 as select distinct* from t_test;

truncate table t_test;

insert into t_test select * from t_test1;

drop table t_test1;

你可能感兴趣的:(oracle数据库)