If everybody has a roleid and based on you can decide ,so
-- roleid 1 = access to 'pending'
-- roleid 2 = access to 'waiting'
declare roleid integer;
select * from
yourordertable t1
where exists ( select 1 from yourordertable t2
where t2.order_id = t1.order_id
and case roleid
when 1 then t2.string_to_match = 'pending'
when 2 then t2.string_to_match = 'waiting'
)
and case roleid when 2 then t2.string_to_match <> 'pending'
not sure about last condition but based on your last output screenshot, it filters out 'pending' status for 'waiting' status .if It is not needed you can remove it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…