GROUP BY Clause
The GROUP BY clause combines results for identical values in a column. This clause is typically used in conjunction with aggregate functions to generate a single figure for each unique value in a column.
For example, to obtain the number of orders for each part number in the orders table:
select partno, count(*) from orders
group by partno;
The preceding query returns one row for each part number in the orders table, even though there may be many orders for the same part number.
Nulls are used to represent unknown data, and two nulls are typically not considered equal in OpenSQL comparisons. However, the GROUP BY clause treats nulls as equal and returns a single row for nulls in a grouped column.
Grouping can be performed on multiple columns. For example, to display the number of orders for each part placed each day:
select odate, partno, count(*) from orders
group by odate, partno;
If the GROUP BY clause is specified, all columns in the SELECT clause must be specified in the GROUP BY clause or be aggregate functions.
Last modified date: 08/28/2024