如何选择那些在任何产品中都没有status = true的品牌

时间:2017-05-16 22:54:25

标签: postgresql

查询

select brand
from products
where status = false
and brand not in (
  select brand
  from products
  where status = true
  group by brand
)
group by brand;

基本上,我想只选择那些在任何产品中都没有status = true的品牌。

我想优化上述查询

2 个答案:

答案 0 :(得分:2)

使用布尔聚合函数bool_or()

select brand
from products
group by brand
having not bool_or(status);

答案 1 :(得分:1)

您可以使用CASE语句将布尔值转换为位,然后使用MAX

SELECT brand
FROM products
GROUP BY brand
HAVING MAX(CASE WHEN status = TRUE THEN 1 ELSE 0 END) = 1