select Distinct a,b from @r temp4 where not exists(
select * from (select a,b from (
select * from (select Distinct a,b from @r
) A,@s ) temp1 where not exists ( select 1 from @r temp2 where temp1.a=temp2.a and temp1.b=temp2.b and temp1.c=temp2.c and temp1.d=temp2.d))
temp3 where temp4.a=temp3.a and temp4.b=temp3.b
)