How do I use T-SQL Group By

后端 未结 5 1514
星月不相逢
星月不相逢 2020-12-06 04:17

I know I need to have (although I don\'t know why) a GROUP BY clause on the end of a SQL query that uses any aggregate functions like count,

5条回答
  •  我在风中等你
    2020-12-06 04:48

    To retrieve the number of widgets from each widget category that has more than 5 widgets, you could do this:

    SELECT WidgetCategory, count(*)
    FROM Widgets
    GROUP BY WidgetCategory
    HAVING count(*) > 5
    

    The "having" clause is something people often forget about, instead opting to retrieve all their data to the client and iterating through it there.

提交回复
热议问题