0

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

0

1 Answer 1

2

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

select id, bool_or(status) status from mytable group by id
Sign up to request clarification or add additional context in comments.

4 Comments

In the output, rows with null are selected?
@rshar: for your sample dataset, this produces exactly the result that you asked for.
This answer should work, because NULL is ignored in aggregate functions. Otherwise, FALSE OR NULL gives NULL.
Above query suggested by GMB works perfectly. My actual data has 50 columns so in order to filter those duplicated rows, how should I give so many columns in group by clause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.