67

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?

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
czupe
  • 4,740
  • 7
  • 34
  • 52
  • Somebody just deleted the post i want to Accept, please wrote it back and i will accept it"SELECT * FROM ( SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*) 'ItemsCount' FROM ItemMetaData GROUP BY ItemMetaData.KEY ) sub WHERE sub.ItemsCount > 2500 ORDER BY sub.ItemsCount desc;" Having is also working and naturally all other answer will vote up, thank you, but... – czupe Jul 09 '12 at 15:08

4 Answers4

120

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;
Bill
  • 4,425
  • 3
  • 21
  • 22
16

You should use having with group functions instead of where. E.g.:

select ..., count(*) from ... group by ... having count(*) > 2500;
Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
8

You do not need to use a subquery - simply use a having clause instead of where clause to filter by an aggregated column.

SELECT
ItemMetaData.KEY, ItemMetaData.VALUE, count(*)
FROM ItemMetaData
GROUP BY ItemMetaData.KEY
HAVING count(*) > 2500
ORDER BY count(*) desc
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

Here is the explanation: WHERE clause introduces a condition on individual rows; HAVING clause introduces a condition on aggregations.

Use WHERE before GROUP BY and HAVING after GROUP BY. It isn't mandatory, but helpuful in most cases.

SELECT 
       ItemMetaData.KEY, ItemMetaData.VALUE, СOUNT(*) 
FROM  ItemMetaData 
GROUP BY
       ItemMetaData.KEY, ItemMetaData.VALUE
HAVING СOUNT(*) > 2500
ORDER BY СOUNT(*) DESC;
Alex M
  • 2,756
  • 7
  • 29
  • 35