dbTalk Databases Forums  

Order by results of count using 'group by'

comp.databases.mysql comp.databases.mysql


Discuss Order by results of count using 'group by' in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
salvador
 
Posts: n/a

Default Order by results of count using 'group by' - 08-01-2006 , 09:11 AM






Hi there,

I'm querying a single table with the goal of selecting the number of
distinct cities of the field 'city' where the count is greater than 9
and ordering by the result by the count. The closest I've been able to
come is this:

'select city,count(1) as list from residential group by city order by
list desc;'

Which will order the full list. Inserting a where statement "where
list > 10" seems to keep resulting in a syntax error. Is it possible
to limit my results in the sql statement, or should I do it in my
programming logic instead?


Reply With Quote
  #2  
Old   
Giuseppe Maxia
 
Posts: n/a

Default Re: Order by results of count using 'group by' - 08-01-2006 , 09:45 AM






salvador wrote:
Quote:
Hi there,

I'm querying a single table with the goal of selecting the number of
distinct cities of the field 'city' where the count is greater than 9
and ordering by the result by the count. The closest I've been able to
come is this:

'select city,count(1) as list from residential group by city order by
list desc;'

Which will order the full list. Inserting a where statement "where
list > 10" seems to keep resulting in a syntax error. Is it possible
to limit my results in the sql statement, or should I do it in my
programming logic instead?

If you want to restrict the search on a condition involving the result of an aggregate function you must use the HAVING
clause instead of the WHERE clause.

select
city, count(*) as list
from
residential
group by
city
having
list > 10
order by
list desc;

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.org/


Reply With Quote
  #3  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Order by results of count using 'group by' - 08-01-2006 , 09:55 AM



salvador wrote:
Quote:
Hi there,

I'm querying a single table with the goal of selecting the number of
distinct cities of the field 'city' where the count is greater than 9
and ordering by the result by the count. The closest I've been able to
come is this:

'select city,count(1) as list from residential group by city order by
list desc;'

Which will order the full list. Inserting a where statement "where
list > 10" seems to keep resulting in a syntax error. Is it possible
to limit my results in the sql statement, or should I do it in my
programming logic instead?

Something like:

SELECT city, count( * ) AS cnt
FROM residential
GROUP BY city
having cnt > 9
order by cnt desc

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================


Reply With Quote
  #4  
Old   
salvador
 
Posts: n/a

Default Re: Order by results of count using 'group by' - 08-02-2006 , 01:49 PM



Hey Jerry and Giuseppe,

Thanks a bunch for your help. This worked great!.

-Sal

Giuseppe Maxia wrote:
Quote:
salvador wrote:
Hi there,

I'm querying a single table with the goal of selecting the number of
distinct cities of the field 'city' where the count is greater than 9
and ordering by the result by the count. The closest I've been able to
come is this:

'select city,count(1) as list from residential group by city order by
list desc;'

Which will order the full list. Inserting a where statement "where
list > 10" seems to keep resulting in a syntax error. Is it possible
to limit my results in the sql statement, or should I do it in my
programming logic instead?


If you want to restrict the search on a condition involving the result of an aggregate function you must use the HAVING
clause instead of the WHERE clause.

select
city, count(*) as list
from
residential
group by
city
having
list > 10
order by
list desc;

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.org/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.