我有以下疑问:

select user_id, trajectory_id, count(trajectory_id) from point group by user_id, trajectory_id

它返回以下结果:
我想删除point表中的所有记录,其中生成的表中的count小于5。
现在我有以下查询,但它不是编译的。有什么问题,我该怎么解决?
delete
from point
where count(trajectory_id) < 5
GROUP BY user_id, trajectory_id

我在用Postgres。

最佳答案

delete p1
from point p1
join
(
  select user_id, trajectory_id
  from point
  group by user_id, trajectory_id
  having count(trajectory_id) < 5
) p2 on p1.user_id = p2.user_id
    and p1.trajectory_id = p2.trajectory_id

关于sql - sql基于分组删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30829076/

10-15 21:16