我有以下查询:
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。
答案 0 :(得分:2)
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
答案 1 :(得分:2)
我会用一个简单的相关子查询来做到这一点:
delete from point
where (user_id, trajectory_id) in (select user_id, trajectory_id
from point
group by user_id, trajectory_id
having count(*) < 5
);
答案 2 :(得分:0)
上面接受的答案有一些语法错误,下面应该可以解决问题,
delete
from point p1
using
(
select user_id, trajectory_id
from point
group by user_id, trajectory_id
having count(trajectory_id) < 5
) p2
where p1.user_id = p2.user_id
and p1.trajectory_id = p2.trajectory_id