删除具有一些重复列值的行

时间:2014-03-20 11:49:04

标签: sql oracle oracle9i

假设我有一个表A列的表格如下:

a  
--
x  
y  
m  
x  
n  
y  

我想删除所有具有重复列值的行,并且只保留一个值。 完成此操作后,我的专栏将如下:

select distinct a from A;

know how to select rows with repeated a column values但我不能只用DELETE替换select,因为它也会删除唯一值 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

在Oracle中,您可以使用隐藏列rowid和相关子查询来执行此操作:

delete from a
    where rowid > (select min(rowid)
                   from a a2
                   where a.a = a2.a
                  );

或者,您可以将其标记为not in

delete from a
    where rowid not in (select min(rowid)
                        from a a2
                        group by a2.a
                       );

答案 1 :(得分:1)

您可以结合使用CTE和Ranking function

;With cte As
(
Select ROW_NUMBER() OVER (PARTITION BY colA ORDER BY colA) as rNum
From yourTable 
)

Delete From cte
Where rNum<>1

答案 2 :(得分:0)

在SQL中,您可以使用CTE并删除重复的行。请参阅下面的查询。

WITH CTE AS(
   SELECT a,
       RN = ROW_NUMBER()OVER(PARTITION BY a ORDER BY a)
   FROM A
)
DELETE FROM CTE WHERE RN > 1