By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
with cte
as(
select 1 as id
union all
select 2
union all
select 3 )
,
cte2 AS
(
select 2 as id
union all
select 3
union all
select 4 )
, cte3 as (
select id from cte
union
select id from cte2
) select cte3.id, case when cte.id is null then 'NO' else 'Yes' end as prestnInA,
case when cte2.id is null then 'NO' else 'Yes' end as prestnInB
from cte3
left join cte on cte3.id=cte.id
left join cte2 on cte3.id=cte2.id
id | prestnInA | prestnInB |
---|---|---|
1 | Yes | NO |
2 | Yes | Yes |
3 | Yes | Yes |
4 | NO | Yes |