By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
with my_table as
(
select 'url1' url, 'desc1' description, 'All' for_country
union all
select 'url2' url, 'desc2' description, 'All' for_country
union all
select 'url2' url, 'desc3' description, 'Germany' for_country
)
select *
from my_table
where (EXISTS (select 1 from my_table where for_country='Germany') and for_country='Germany') OR
(NOT EXISTS (select 1 from my_table where for_country='Germany') and for_country='All')
url | description | for_country |
---|---|---|
url2 | desc3 | Germany |