group by building, locationできます 行のwhere object in ('WALL', 'WINDOW') :
select building, location, 'FLAG' action
from tablename
where object in ('WALL', 'WINDOW')
group by building, location
having count(distinct object) < 2
条件count(distinct object) < 2 having 句は、building, locationの組み合わせを返します ここで、'WALL' および'WINDOW' 両方が存在するわけではありません。
結果:
| building | location | action |
| -------- | -------- | ------ |
| A | FLOOR2 | FLAG |
| B | FLOOR1 | FLAG |
または存在しない場合:
select t.building, t.location, 'FLAG' action
from tablename t
where object in ('WALL', 'WINDOW')
and not exists (
select 1 from tablename
where building = t.building and location = t.location and object <> t.object
)
デモ をご覧ください。 。