I have a table in Postgres that looks something like this:
x
table
------
id varchar(10)
reversed boolean
status varchar(3)
group_id varchar(10)
The algorithm is if any single record has reversed = true
or status = foo
, all records with the same group_id
should be filtered. If group_id
is null, which is possible, I’d like to keep those records regardless of reversed
or status
The closest I’ve gotten is with a NOT IN
subquery:
select *
from
table
where
group_id
not in (
select
group_id
from
records
where
reversed = true
or
status = foo
)
but NOT IN
also filters nulls naturally which i don’t want. have also tried not exists and inner joins but haven’t quite gotten the right syntax. any help would be greatly appreciated!
Advertisement
Answer
Try the following by adding group_id is null
in where
condition,
select *
from table
where group_id not in
(
select
group_id
from records
where (reversed = true or status = foo)
)
or group_id is null