PostgreSQL查询无匹配条件

时间:2015-07-10 18:34:36

标签: sql postgresql

我有一张表(约有1亿行),如下所示

identifier       bigint
active           boolean
extraInformation character varying(100)

对于每个标识符,数据可以并且将有几行其中active为false,但对于每个标识符,必须始终只有一个active为true。

有一个错误导致某些标识符的所有活动标志都设置为false。

因此我需要一个如下查询:

显示所有活动标记设置为false的所有标识符

1 个答案:

答案 0 :(得分:5)

如果您使用的是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