Contents Index How queries with GROUP BY are executed WHERE clause and GROUP BY

ASA SQL User's Guide
  Summarizing, Grouping and Sorting Query Results
    Understanding GROUP BY

GROUP BY with multiple columns


You can list more than one expression in the GROUP BY clause in order to nest groups—that is, you can group a table by any combination of expressions.

The following query lists the average price of products, grouped first by name and then by size:

SELECT name, size, AVG(unit_price)
FROM product
GROUP BY name, size
name size AVG(product.unit_price)
Tee Shirt Small 9
Tee Shirt Medium 14
Tee Shirt One size fits all 14
Baseball Cap One size fits all 9.5
... ... ...
Columns in GROUP BY that are not in the select list 

A Sybase extension to the SQL/92 standard that is supported by both Adaptive Server Enterprise and Adaptive Server Anywhere is to allow expressions to the GROUP BY clause that are not in the select list. For example, the following query lists the number of contacts in each city:

SELECT state, count(id)
FROM contact
GROUP BY state, city

Contents Index How queries with GROUP BY are executed WHERE clause and GROUP BY