Я думаю, что вы хотите вернуть отличный id
, при условии, что он имеет оба случая (N
и Y
) одновременно для столбца isPresent
. Итак, рассмотрите возможность использования следующего оператора SQL с оператором exists
, изменив параметры (теперь это 2
):
with tab(id, state, isPresent) as
(
select 'id1', 1, 'N' union all
select 'id2', 2, 'N' union all
select 'id2', 2, 'Y' union all
select 'id3', 3, 'N' union all
select 'id4', 4, 'N' union all
select 'id4', 4, 'Y' union all
select 'id5', 2, 'N' union all
select 'id5', 2, 'Y'
)
select distinct id
from tab t1
where exists ( select 1
from tab t2
where t2.state = t1.state
and t2.isPresent in ('N','Y') -- this line might be commented out, if it's certain that there're only two cases "N" and "Y"
group by t2.state
having count(distinct t2.isPresent) = 2
)
and t1.state = 2 -- try other values also such as 1, 3, 4
Для значений 1
и 3
строка не возвращается, а для 2
и 4
возвращается хотя бы одна строка.
Rextester Demo