简体   繁体   English

根据 PostgreSQL 中 where 语句中的多个条件过滤行

[英]Filter rows based on multiple condition in where statement in PostgreSQL

I have following table in Postgres 11.0我在 Postgres 11.0 中有下表

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

The id values are duplicated due to different value in status column.由于状态列中的值不同,id 值重复。 I would like to keep the rows with either true or false.我想保留对或错的行。 If multiple rows have null value, I would like to keep only 1 row.如果多行有 null 值,我想只保留 1 行。 The desired output is:所需的 output 是:

id              status
NCT00632827 true
NCT00770185 false
NCT00784303 (null)

I am trying following query:我正在尝试以下查询:

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

How can I keep rows with null value (last row in the desired output)?如何保留具有 null 值的行(所需输出中的最后一行)?

For this dataset, simple aggregation seems good enough to produce the expected results:对于这个数据集,简单的聚合似乎足以产生预期的结果:

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

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

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