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)
 Single expression in distinct list

Author  Topic 

MichelleMA
Starting Member

32 Posts

Posted - 2009-01-22 : 10:58:00
Hi, can someone please suggest a way to do this query, I know only one expression can be specified in the select distinct list, but I can't figure out how to isolate the distinct A, B columns combination from the table without creating a temp table:

SELECT A, COUNT(B) FROM TABLEX
GROUP BY A
HAVING COUNT(B)>1
AND A IN (SELECT DISTINCT A, B FROM TABLEX)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-22 : 11:00:21
did you mean this?

SELECT A, COUNT(DISTINCT B) FROM TABLEX
GROUP BY A
HAVING COUNT(DISTINCT B)>1
Go to Top of Page

MichelleMA
Starting Member

32 Posts

Posted - 2009-01-22 : 13:06:45
Yes, thank you! Is there a way to return the B column in the selection?

Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-22 : 13:10:21
yup. like this

SELECT t1.A,t1.B,t2.BDistCount
FROM TABLEX t1
INNER JOIN
(
SELECT A, COUNT(DISTINCT B) AS BDistCount FROM TABLEX
GROUP BY A
HAVING COUNT(DISTINCT B)>1
)t2
ON t2.A=t1.A
Go to Top of Page

MichelleMA
Starting Member

32 Posts

Posted - 2009-01-22 : 17:10:50
That's exactly what I needed! Thank you so much for your help!
Go to Top of Page
   

- Advertisement -