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?
And the short answer to the above question is, no. You can't. It is not possible to nest aggregate functions.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With