GROUP BY Clause (SQL Server Compact)

Specifies the groups (equivalence classes) that output rows are to be placed in. If aggregate functions are included in the SELECT clause <select list>, the GROUP BY clause calculates a summary value for each group.

Syntax

[ GROUP BY group_by_expression [ ,...n ] ] 

Arguments

  • group_by_expression
    An expression that grouping is performed on. The group_by_expression argument is also known as a grouping column. For more information, see Remarks.

Remarks

The group_by_expression parameter can be a column or a nonaggregate expression that references a column. A column alias that is defined in the select list cannot be used to specify a grouping column. Aggregate expressions cannot be specified in a group_by_expression.

Note

Columns of type ntext and image cannot be used in group_by_expression.

When GROUP BY is specified, the GROUP BY expression must exactly match the select list expression, or each column in any nonaggregate expression in the select list must be included in the GROUP BY list.

Note

GROUP BY ALL is not supported in SQL Server Compact 3.5.

Example

The following example returns a list of all orders grouped by product ID.

SELECT [Product ID], COUNT(*) AS Expr1 FROM [Order Details] GROUP BY [Product ID]