Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL COUNT* GROUP BY bigger than,

I want to select the distinct keys with the occurence number, this query seems functionate:

SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*)  FROM ItemMetaData  GROUP BY ItemMetaData.KEY  ORDER BY count(*) desc; 

But I also want to filter these result, meaning I want only where count(*) is greater than 2500 so only bigger than 2500 occurence will shown, but:

SELECT *  FROM  (     SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*)      FROM ItemMetaData      GROUP BY ItemMetaData.KEY      ORDER BY count(*) desc ) as result WHERE count(*)>2500; 

Unfortunately this query results in a syntax error. Can you help me achieve my requirement?

like image 926
czupe Avatar asked Jul 09 '12 15:07

czupe


People also ask

Can I do a max count * in SQL?

And the short answer to the above question is, no. You can't. It is not possible to nest aggregate functions.

Can we use count and GROUP BY together?

The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

How do I count after GROUP BY in SQL?

SQL – count() with Group By clause The count() function is an aggregate function use to find the count of the rows that satisfy the fixed conditions. The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.


1 Answers

HAVING clause for aggregates

SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*)  FROM ItemMetaData  Group By ItemMetaData.KEY, ItemMetaData.VALUE HAVING count(*) > 2500 ORDER BY count(*) desc; 
like image 149
Bill Avatar answered Oct 05 '22 21:10

Bill