记得以前有个同事问过我这个,说是以前面试的时候碰到的问题,下面我介绍三种方法。
首先我们在这里创建一个测试表添加相应的测试数据。
create table test (id number,name varchar(10));
insert into test values(1,'liufang');
insert into test values(2,'xiaozhang');
insert into test values(3,'dawei');
insert into test values(4,'laotan');
insert into test values(5,'laotan');
insert into test values(6,'laotan');
insert into test values(7,'dawei');
1、根绝name相同 ID不同来的方式来判断(id必须唯一)
delete from test a where exists (select null from test b where b.name=a.name and b.id>a.id);
2、用rowid 来代替其中的id,比上面的方法更适用,没有字段唯一限制
delete from test a where exists (select null from test b where b.name=a.name and b.rowid>a.rowid);
3、 通过分析函数根据name 分组生成序号,然后删除序号大于1 的数据
(注:rder by 2,3根据查询的第二第三个字段升序排序查询)
select rowid as rid,name,row_number() over(partition by name order by id) from test order by 2,3;
分析函数会吧对应名称相同的字段出现的次数记录下来,把对应次数大于1 的数据删除即可
分析函数:ROW_NUMBER() OVER (partition by name order by name) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,
这里的ROW_NUMBER() OVER (partition by name order by name) 是先把name列升序,再为降序以后的没条name记录返回一个序号,
delete from test
where rowid in (select rowid
from (select rowid as rid,
row_number() over(partition by name order by id) as seq
from test)
where seq > 1);