使用GROUP BY删除

时间:2012-04-06 16:53:06

标签: sql sql-server-2008 tsql

如何更改此SELECT以便它是用于删除的标准?

SELECT COUNT(empid), empid, status, deptid from tableA 
GROUP BY empid, status, deptid HAVING status is null and deptid = 5

4 个答案:

答案 0 :(得分:2)

你想删除什么?

如果仅使用

在deptId 5中记录具有NULL状态的记录
WHERE status is NULL and DeptID = 5

如果您正在寻找更多信息,那就不太清楚......

答案 1 :(得分:1)

使用GROUP BY删除可能无法正常工作,我猜。

您可以尝试以下操作,请查看它是否有效。

DELETE 
FROM tableA
WHERE status is null AND deptid=5

答案 2 :(得分:1)

DELETE FROM tableA 
WHERE status is null AND deptid = 5 

答案 3 :(得分:1)

Delete from tableA a, 
(SELECT COUNT(empid) as count, empid, status, deptid from tableA 
    GROUP BY empid, status, deptid HAVING status is null and deptid = 5) as b 
where a.empid = b.empid
and a.status = b.status
and a.deptid = b.deptid
and b.count > 5

这样的东西会是你想要的吗?它将删除属于该组的所有记录,其数量为> 5

否则,您实际上并没有从组中删除值,可以使用简单的删除;

Delete from tableA 
where status is null 
and deptid = 5