删除重复记录而不使用中间表

时间:2014-03-03 13:07:53

标签: sql teradata

我想从数据库中的表中删除所有重复数据,而不使用行id或中间表。有没有办法做到这一点?

这是我的初始表:

emp emp_name    emp_addr
2   B   Pune1
3   C   Pune2
1   A   Pune
2   B   Pune1
3   C   Pune2
1   A   Pune
2   B   Pune1
3   C   Pune2
1   A   Pune
2   B   Pune1
1   A   Pune
1   A   Pune

删除重复数据应该是什么样子:

emp emp_name    emp_addr
2   B   Pune1
3   C   Pune2
1   A   Pune

2 个答案:

答案 0 :(得分:0)

您可以尝试ROW_NUMBER,即

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

由于 苏雷什

答案 1 :(得分:-1)

delete from table where                
EmpID in(select EmpID from table group by EmpId having count(*) >1)

如果您的 RDBMS

支持,则类似这样的内容
;with cte as
(
select *,rn=row_number()over(order by empID) from table
)

delete from cte where rn>1