我想做一些如下的事情:
DELETE UserPredictions
GROUP BY UserId
HAVING COUNT(*) < 500
但是我得到了一个语法错误。是否可以在SQL Server中使用HAVING子句执行delete操作,或者我是否必须将计数汇总到CTE中并使用join执行delete操作?
发布于 2012-07-29 01:52:29
不怎么有意思。having子句意味着聚合,这意味着您不再拥有原始行。
我认为你需要以下几点:
DELETE from UserPredictions
where UserId in (select UserId from UserPredictions group by UserId having count(*) < 500)
发布于 2012-07-29 02:57:33
可以在DELETE
语句中使用联接的subselect:
DELETE a
FROM UserPredictions a
JOIN
(
SELECT UserId
FROM UserPredictions
GROUP BY UserId
HAVING COUNT(1) < 500
) b ON a.UserId = b.UserId
发布于 2012-07-29 01:53:17
尝试此嵌套查询:
DELETE FROM UserPredictions
WHERE UserId IN (SELECT UserId
FROM UserPredictions
GROUP BY UserId
HAVING COUNT(*) < 500)
https://stackoverflow.com/questions/11705996
复制