简体   繁体   English

PostgreSQL查询无匹配条件

[英]PostgreSQL query none match criteria

I have a table (with about 100 million rows) as follows 我有一个表(大约有1亿行),如下所示

identifier       bigint
active           boolean
extraInformation character varying(100)

The data can and will have several rows where active is false for every identifier, but there must always be exactly one where active is true for every identifier. 数据可以并且将有几行,其中每个标识符的active均为false,但是对于每个标识符,总是必须存在一行且true为true的行。

There was a bug that caused all active flags to be set to false for certain identifiers. 有一个错误导致某些标识符的所有活动标志都设置为false。

I therefore need a query as follows: 因此,我需要如下查询:

Show all identifiers that have all their active flags set to false 显示所有将所有活动标志设置为false的标识符

If you are on 9.4, you can use this: 如果您使用的是9.4,则可以使用以下命令:

select identifier 
from the_table
group by identifier
having count(*) = count(*) filter (where not active);

For older versions: 对于旧版本:

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 SQLFiddle: http ://sqlfiddle.com/#!15/7423e/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM