如何只保留第一个唯一元素并在oracle中删除其重复项?

时间:2020-03-27 19:20:22

标签: sql oracle

因此,此查询的语句看起来非常简单,但实际上并不那么容易。 香港专业教育学院尝试过的继承人代码。

Delete from table where id
In (select id from (select 
 id, row_number() 
  over(partition by id) 
rn from table where rn>1)

上面的方法可以工作,但是多数其他数据库可能不支持几乎所有数据库(例如partition by)的标准sql。我在下面尝试的是可以使用group by。我在下面尝试过,但是我不确定这是否可行。任何建议以及哪个建议已优化

  //using group by    
Delete from table where id
In (select id from(select 
 id from table 
  Group by id 
  Having sum(1)>1) 

  )

2 个答案:

答案 0 :(得分:2)

正如问题所述

在oracle中删除其重复项

然后

delete from your_table a
where a.rowid > (select min(b.rowid)
                 from your_table b
                 where b.id = a.id
                );

答案 1 :(得分:1)

您可以按以下方式使用exists

Delete from your_table t
Where exists (select 1 from your_table t1
Where t1.id = t.id
And t1.rowid > t.rowid)