SQL COUNT* GROUP BY bigger than,

前端 未结 4 2254
猫巷女王i
猫巷女王i 2020-12-14 14:36

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

SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*) 
FROM ItemM         


        
相关标签:
4条回答
  • 2020-12-14 14:51

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

    select ..., count(*) from ... group by ... having count(*) > 2500;
    
    0 讨论(0)
  • 2020-12-14 15:00

    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;
    
    0 讨论(0)
  • 2020-12-14 15:02

    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
    
    0 讨论(0)
  • 2020-12-14 15:05

    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;
    
    0 讨论(0)
提交回复
热议问题