我创建了一个查询以删除表中的重复行。我尝试这样做,但是输出是“太多值”
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;
它不起作用
答案 0 :(得分:5)
您标记为plsql的我知道您的DB
是Oracle
。因此,您可以按以下方式使用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
);