In Your example data isn't b and g valid results as well ???Here You go, try this:select 'a' as c,1 as ninto #tunion all select 'a',2union all select 'b',3union all select 'c',1union all select 'd',2union all select 'e',1union all select 'e',2union all select 'g',3/* comment this line with "--" to add additional testdata belowunion all select 'x',6union all select 'x',44union all select 'x',7union all select 'y',6union all select 'y',44union all select 'y',7--*/select t1.c as c1 ,t2.c as c2from #t t1 join #t t2 on t1.c <> t2.cwhere t1.n = t2.ngroup by t1.c ,t2.chaving count(*) = (select count(*) from #t t3 where t1.c = t3.c group by t3.c) and count(*) = (select count(*) from #t t3 where t2.c = t3.c group by t3.c) --and count(*) >= 1 -- #of matching records per groupdrop table #t
rockmoose