首页 > 解决方案 > 在 PostgreSQL 中的 where 语句中根据多个条件过滤行

问题描述

我在 Postgres 11.0 中有下表

id              status
NCT00632827 true
NCT00632827 (null)
NCT00770185 false
NCT00770185 (null)
NCT00784303 (null)
NCT00784303 (null)

由于状态列中的值不同,id 值重复。我想保留对或错的行。如果多行有空值,我想只保留 1 行。所需的输出是:

id              status
NCT00632827 true
NCT00770185 false
NCT00784303 (null)

我正在尝试以下查询:

select id, status
where status = 'false' or status  = 'true'
from table

如何保留具有空值的行(所需输出中的最后一行)?

标签: sqlpostgresqlselectwhere-clause

解决方案


对于这个数据集,简单的聚合似乎足以产生预期的结果:

select id, bool_or(status) status from mytable group by id

推荐阅读