Я бы пошел на group by
и having
. Один из методов:
select id
from t
group by id
having sum(case when cola <> 'YD' then 1 else 0 end) > 0;
Однако, другой подход может быть:
select distinct id
from t
where exists (select 1 from t t2 where t2.id = t.id and t2.cola <> 'YD');
Или:
select distinct id
from t
where cola <> 'YD;
Трудно сказать, какой из них будет самым быстрым. Если у вас есть отдельная таблица идентификаторов, то лучшим подходом будет:
select ids.id
from ids
where exists (select 1 from t t2 where t2.id = ids.id and t2.cola <> 'YD');
с индексом t(id, cola)
.