Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » Summarizing, Grouping, and Sorting Query Results » The GROUP BY clause: organizing query results into groups

The GROUP BY clause: organizing query results into groups Next Page

Using GROUP BY with aggregate functions


A GROUP BY clause almost always appears in statements that include aggregate functions, in which case the aggregate produces a value for each group. These values are called vector aggregates. (Remember that a scalar aggregate is a single value produced by an aggregate function without a GROUP BY clause.)

Example

The following query lists the average price of each kind of product:

SELECT Name, AVG( UnitPrice ) AS Price
   FROM Products
   GROUP BY Name;
name Price
Tee Shirt 12.333333333
Baseball Cap 9.5
Visor 7
Sweatshirt 24
... ...

The summary values (vector aggregates) produced by SELECT statements with aggregates and a GROUP BY appear as columns in each row of the results. By contrast, the summary values (scalar aggregates) produced by queries with aggregates and no GROUP BY also appear as columns, but with only one row. For example:

SELECT AVG( UnitPrice )
   FROM Products;
AVG(Products.UnitPrice)
13.3