By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
with cte1 as
(
select 1 as id, 'p-01' as code, 'OK' as val
union all
select 2 , 'p-01' , 'NOT OK'
union all
select 3 , 'p-02' , 'OK'
union all
select 4 , 'p-02' , 'OK'
)
SELECT distinct code FROM cte1 a
WHERE NOT exists (SELECT 1 FROM cte1 b where a.code=b.code and val = 'NOT OK')
code |
---|
p-02 |
select code from cte1
group by code
having sum(case when value_check='NOT OK' then 1 else 0 end)=0
Msg 208 Level 16 State 1 Line 1
Invalid object name 'cte1'.