Bạn có thể group by building, location cho các hàng 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
Điều kiện count(distinct object) < 2 trong having mệnh đề trả về kết hợp của building, location ở đâu 'WALL' và 'WINDOW' không tồn tại cả hai.
Xem bản trình diễn
.
Kết quả:
| building | location | action |
| -------- | -------- | ------ |
| A | FLOOR2 | FLAG |
| B | FLOOR1 | FLAG |
Hoặc với KHÔNG TỒN TẠI:
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
)
Xem bản trình diễn .