以下为通过
row_number() over(...)删除重复数据的例子,仅供参考:
delete from acc_fundnav
where rowid in (select row1
from (select rowid row1,
row_number() over(partition by HOST_ID order by rowid) lev
from acc_fundnav)
delete from acc_fundnav
where rowid in (select row1
from (select rowid row1,
row_number() over(partition by HOST_ID order by rowid) lev
from acc_fundnav)
where lev > 1)
注释:HOST_ID应该替换成表的所有列,这样才可以达到将各自重复的行归为一组的目的。
⑵ 利用rowid结合max或min函数
使用 rowid快速唯一 确定重复行结合max或min函数来实现删除重复行。
SQL>delete from stu a where rowid not in (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex); //这里max使用min也可以
或者用下面的语句
SQL>delete from stu a where rowid < (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex); //这里如果把max换成min的话,前面的where子句中需要把"<"改为">"
使用 rowid快速唯一 确定重复行结合max或min函数来实现删除重复行。
SQL>delete from stu a where rowid not in (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex); //这里max使用min也可以
或者用下面的语句
SQL>delete from stu a where rowid < (select max(b.rowid) from stu b where a.no=b.no and a.name = b.name and a.sex = b.sex); //这里如果把max换成min的话,前面的where子句中需要把"<"改为">"
跟上面的方法思路基本是一样的,不过使用了
group by,减少了显性的比较条件,提高效率。
SQL>delete from stu where rowid not in (select max(rowid) from stu t group by t.no, t.name, t.sex );
SQL>delete from stu where rowid not in (select max(rowid) from stu t group by t.no, t.name, t.sex );
思考:若在stu表中唯一确定任意一行数据(1, 'ab','男'),把sex字段更新为"女",怎么做?
SQL>update stu set sex='女' where rowid=(select min(rowid) from stu where no=1 and name='ab' and sex='男');
SQL>update stu set sex='女' where rowid=(select min(rowid) from stu where no=1 and name='ab' and sex='男');
oracle伪列 rowid和rownum