Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help. COUNT with DISTINCT

Status
Not open for further replies.

wolves

Programmer
Jan 26, 2001
130
US
When I run this query, I get the result that I need.

Code:
Select PHONE, COUNT(DISTINCT(NAME)) from TABLE
where PHONE = '1234567879'
group PHONE
Having COUNT(DISTINCT(NAME)) > 1

PHONE                  COUNT(DISTINCT(NAME))
-------------------- ---------------------------
123456789                                      3


But, when I run this query, I am trying to also list the NAMES that are distinct, that are > 1, why do I get NO ROWS RETURNED?

Code:
Select NAME, PHONE, COUNT(DISTINCT(NAME)) from TABLE
where PHONE = '123456789'
group by NAME, PHONE
Having COUNT(DISTINCT(NAME)) > 1

no rows selected
 
When you group by name and phone, your count of DISTINCT(name) will automatically be 1.

Try this:
Code:
SELECT name, phone
FROM table t, (Select PHONE, COUNT(DISTINCT(NAME)) 
                 from  TABLE
                group PHONE
               Having COUNT(DISTINCT(NAME)) > 1) v
WHERE t.phone = v.phone
ORDER BY 2,1;
 
Thanks, will give it a try and see how it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top