Użyj CTE, które zwraca wszystkie wiersze, w których A = C
i dołącz do stołu:
with cte as (
select * from tablename
where "A" = "C"
)
select distinct t."A", t."B", c."D"
from tablename t left join cte c
on c."A" = t."A" and c."B" = t."B"
order by t."A", t."B"
Zobacz demo
.
Wyniki:
| A | B | D |
| --- | --- | --- |
| x | 0 | 1 |
| x | 1 | |
| y | 0 | |
| y | 1 | 0 |