[英]Postgresql - Filter out data within a single query
I have the following data in the booking table.我在预订表中有以下数据。
booking预订
id email price type areaid
1 person1@test1.com 70 type1 1
2 person2@test2.com 60 type2 2
3 person3@test3.com 50 type1 3
4 person4@test4.com 110 type1 3
5 person1@test1.com 90 type2 4
6 person2@test2.com 65 type2 1
7 person3@test3.com 84 type2 2
8 person4@test4.com 84 type1 2
I need to retrieve all email addresses from booking table which have only type2
and no other types.我需要从只有type2
而没有其他类型的预订表中检索所有 email 地址。 According to the data, only person2
meets this requirement.根据资料,只有person2
符合这个要求。
How can I achieve this within a single query?如何在单个查询中实现此目的?
You can use HAVING
:您可以使用HAVING
:
select email
from t
group by email
having min(type) = 'type2' and max(type) = 'type2'
Select b.email
from booking b
group by b.email
having
'type2'= any(array_agg(b.type)) and array_length(array_agg(b.type),1)=1;
You can use above query for your task.您可以使用上面的查询来完成您的任务。 First group by clause using email then use having clause to filter the results after get grouped.首先使用 email group by 子句,然后使用 having 子句过滤 get 分组后的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.