group by and SQL standards  Referencing other columns in queries using group by

Chapter 3: Using Aggregates, Grouping, and Sorting

Nesting groups with group by

You can nest groups by listing more than one column in the group by clause. Once the sets are established with group by, the aggregates are applied. This statement finds the average price and the sum of book sales, grouped first bypublisher identification number, then by type:

select pub_id, type, avg(price), sum(total_sales)
from titles
group by pub_id, type
pub_id type
------ ------------ ------ -------
0736 business 2.99 18,722
0736 psychology 11.48 9,564
0877 UNDECIDED NULL NULL
0877 mod_cook 11.49 24,278
0877 psychology 21.59 375
0877 trad_cook 15.96 19,566
1389 business 17.31 12,066
1389 popular_comp 21.48 12,875(8 rows affected)

You can nest groups within groups. The maximum number of group by columns (or expressions) is not explicitly limited. The only limit of group by results is that the width of the group by columns plus the aggregate results be no greater than 64K.





Copyright © 2005. Sybase Inc. All rights reserved. Referencing other columns in queries using group by

View this book as PDF