在SQL中复制行会输出太多值

时间:2018-10-21 07:59:04

标签: sql oracle sql-delete

我创建了一个查询以删除表中的重复行。我尝试这样做,但是输出是“太多值”

DELETE FROM Employeetest 
WHERE employeeid IN (SELECT 
                         employeetest.*, 
                         ROW_NUMBER() OVER (PARTITION BY employeeid ORDER BY employeeid) AS rownumber 
                     FROM
                         employeetest 
                     HAVING
                         (rownumber > 1)); 

我也是

SELECT
    employeetest.*, 
    ROW_NUMBER() OVER (PARTITION BY employeeid ORDER BY employeeid) AS rownumber 
FROM
    employeetest

然后

DELETE * FROM employeetest;

它不起作用

3 个答案:

答案 0 :(得分:5)

您标记为plsql的我知道您的DBOracle。因此,您可以按以下方式使用rowid pseudocolumn

delete Employeetest t1
where rowid <
(
select max(rowid)
  from Employeetest t2 
 where t2.employeeid = t1.employeeid 
);

如果目标是删除employeeid个值的所有重复项。

P.S。无法以这种方式Delete * from employeetest where ...进行删除,但是可以使用Delete from employeetest where ...Delete employeetest where ...

答案 1 :(得分:1)

我总是这样使用:

delete employeetest
where  rowid in
       ( select lag(rowid) over (partition by employeeid order by null) 
         from employeetest )

答案 2 :(得分:0)

此逻辑通常也写为:

delete Employeetest 
where rowid in (select max(rowid)
                from Employeetest e2
                group by e2.employeeid
               );