dbTalk Databases Forums  

Query Involving Group

comp.databases.mysql comp.databases.mysql


Discuss Query Involving Group in the comp.databases.mysql forum.



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

Default Query Involving Group - 05-12-2011 , 12:31 PM






Hi,

If I do the query:
SELECT COUNT(*) FROM Persons GROUP BY LastName;
I get a listing giving the count from each group.

I want to determine the largest group so I tried:
SELECT MAX(COUNT(*)) FROM Persons GROUP BY LastName;
The above query, according to MySQL, is not valid.

I would appreciate help.

Thank you,
Joe

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Query Involving Group - 05-12-2011 , 12:59 PM






On 12-05-2011 19:31, Joseph Hesse wrote:
Quote:
Hi,

If I do the query:
SELECT COUNT(*) FROM Persons GROUP BY LastName;
I get a listing giving the count from each group.

I want to determine the largest group so I tried:
SELECT MAX(COUNT(*)) FROM Persons GROUP BY LastName;
The above query, according to MySQL, is not valid.

I would appreciate help.

Thank you,
Joe

SELECT MAX(cnt)
FROM ( SELECT COUNT(*) cnt
FROM Persons
GROUP BY LastName ) t


--
Luuk

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Query Involving Group - 05-12-2011 , 01:11 PM



On 2011-05-12 19:31, Joseph Hesse wrote:
Quote:
Hi,

If I do the query:
SELECT COUNT(*) FROM Persons GROUP BY LastName;
I get a listing giving the count from each group.

I want to determine the largest group so I tried:
SELECT MAX(COUNT(*)) FROM Persons GROUP BY LastName;
The above query, according to MySQL, is not valid.
Even if it where valid, the meaning would be kinda weird. Since you are
grouping by lastname, you would get max per lastname, which in turn
would be the same as count.

The result of:

SELECT COUNT(*) as cnt
FROM Persons
GROUP BY LastName

is a new table so you can select from that:

select max(cnt) from (
SELECT COUNT(*) as cnt
FROM Persons
GROUP BY LastName
) as T

/Lennart

Reply With Quote
  #4  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Query Involving Group - 05-12-2011 , 01:21 PM



Joseph Hesse <joehesse (AT) gmail (DOT) com> wrote:
Quote:
If I do the query:
SELECT COUNT(*) FROM Persons GROUP BY LastName;
I get a listing giving the count from each group.

I want to determine the largest group so I tried:
SELECT MAX(COUNT(*)) FROM Persons GROUP BY LastName;
The above query, according to MySQL, is not valid.
SELECT LastName,
COUNT(*) AS cnt
FROM Persons
GROUP BY LastName
ORDER BY cnt DESC
LIMIT 1;

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.