我想保留重复的行并删除其余的行
例如
Emp_no Leave_cd
-----------------
1234 AL
1234 AL
1234 AL
1234 BT
2345 AL
2345 AL
2345 AL
2345 AL
2345 EL
我想根据leave_cd
和emp_no
删除非重复行。
我试过这段代码,但代码删除了表中的所有行
Delete from table
where LEAVE_CD IN (Select leave_cd as d
from table
group by EMP_NO, LEAVE_CD
having Count(table.leave_cd) = 1)
答案 0 :(得分:1)
DELETE Table
--SELECT t1.* -- uncomment this line for a preview
FROM Table t1
INNER JOIN (SELECT g2.Emp_no, g2.Leave_cd
FROM Table g2
GROUP BY g2.Emp_no, g2.Leave_cd
HAVING COUNT(g2.Emp_no) = 1 ) z3 ON z3.Emp_no = t1.Emp_no
AND z3.LEave_cd = t1.Leave_cd
我确信您有一个特定的用例,这对您有意义。但是,我绝不会根据子查询删除代码,无论一般的做法多么微不足道。