ASA SQL User's Guide
Summarizing, Grouping and Sorting Query Results
Understanding GROUP BY
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 |
... | ... | ... |
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