GROUP BY Clause

Restriction: This topic applies to Windows environments only.

When a query statement includes a GROUP BY clause, the SELECT clause for that query may list (in addition to ordinary column names) one or more aggregate functions (SUM, COUNT, AVG, etc.) operating on groups of data values in other columns.

The GROUP BY clause contains a column-list argument that must include all simple column names that appear in the SELECT clause. Columns can also be named using the AS Clause (see SELECT Clause). Simple column names are called grouping columns, since they group together records with identical data in that column. This record grouping allows mathematical operations to be performed on the other columns specified as arguments in the aggregate functions. Records can also be grouped by any valid expression appearing in the SELECT clause argument. In these cases, the expression must also be referenced in the GROUP BY clause by a whole number representing its relative column position in the SELECT clause (or output result).

Syntax

The GROUP BY clause format appears below:

GROUP BY column-list

where:

column-list Consists of an ordered list of column names or numbers, separated by commas.