Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 receive unique result from multiple rows

Author  Topic 

hitushah
Starting Member

1 Post

Posted - 2005-06-16 : 16:14:23
Hi,

i have a table like,

==========
a | 1
a | 2
b | 3
c | 1
d | 2
e | 1
e | 2
g | 3
==========

I need a final result like,

a (which contains 1 and 2)
e (which contains 1 and 2 same as a)

how can i do it?

thanks in advance,
Hitesh

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-06-16 : 17:16:06
In Your example data isn't b and g valid results as well ???

Here You go, try this:
select 'a' as c,1 as n
into #t
union all select 'a',2
union all select 'b',3
union all select 'c',1
union all select 'd',2
union all select 'e',1
union all select 'e',2
union all select 'g',3
/* comment this line with "--" to add additional testdata below
union all select 'x',6
union all select 'x',44
union all select 'x',7
union all select 'y',6
union all select 'y',44
union all select 'y',7
--*/


select
t1.c as c1
,t2.c as c2
from
#t t1 join #t t2
on t1.c <> t2.c
where
t1.n = t2.n
group by
t1.c
,t2.c
having
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 group


drop table #t


rockmoose
Go to Top of Page
   

- Advertisement -