简体   繁体   中英

Filter rows based on multiple condition in where statement in PostgreSQL

I have following table in 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. 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. The desired output is:

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)?

For this dataset, simple aggregation seems good enough to produce the expected results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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