|
|
Grouped Set Functions - the GROUP BY Clause
Normally, set functions return a single value, calculated from the set of all values in the column or expression.
If the
SELECTstatement includes aGROUP BYclause, set functions will be applied to groups of values. Columns used forGROUP BYdo not have to be included in theSELECTlist.Find the number of rows in each category within the FORMATS table:
SELECT category_id, COUNT(display_order) FROM formats GROUP BY category_id;Returns:
Restrictions when Using GROUP BY
Using a
GROUP BYclause places some restrictions on theSELECTstatement. Only constants, columns used in theGROUP BYclause, and columns used in set functions may be included in theSELECTlist.Column References
In a statement with column references in the
SELECTlist, all columns not used in set functions must be used as grouping columns.Null Values
For grouping purposes,
NULLvalues are regarded as equivalent. Thus for the example table:The following statement:
SELECT sample, COUNT(*) as number ... GROUP BY sample;Returns:
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|