我有一个表(大约有1亿行)如下
identifier bigint
active boolean
extraInformation character varying(100)
数据可以并且将会有几行,其中每个标识符的 active 为 false,但对于每个标识符,必须始终有一行 active 为 true。
存在一个错误,导致某些标识符的所有事件标志都设置为 false。
因此我需要如下查询:
显示所有事件标志均设置为 false 的所有标识符
最佳答案
如果您使用的是 9.4,则可以使用此:
select identifier
from the_table
group by identifier
having count(*) = count(*) filter (where not active);
对于旧版本:
select identifier
from the_table
group by identifier
having count(*) = count(case when not active then 1 end);
SQLFiddle:http://sqlfiddle.com/#!15/7423e/2
关于sql - PostgreSQL 查询无匹配条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31347953/