Multi-Level Group Calculations
The example of a Group By clause in Getting Started,
Group and Analyze Rows, used a two-level group to segment stock data into sets by symbol and then year within symbol. The order of the columns within the Group By column expression defines subgrouping.
Once groups are defined, you determine what analysis occurs and is returned using analytic functions in the Select clause. Each group returns one row of data, so the functions you choose must be aggregate analytic functions that return a single value for all rows in the group. See
Built-In Analytic Functions: Aggregate and Window for the list of built-in
Presto analytic functions that you may use with Group By clauses.
For more examples, see:
Single Level Group
You can use a single group level such as this example which counts the number of legislators in each house of the US Congress:
Three Group Levels
With each level of group, you add columns to the expression. This example groups stock data by symbol, year and quarter within year to derive the average low price for each quarter:
The columns that appear in the Select clause must either be used for group in the Group By clause or be used in aggregate calculations. It is also quite common to sort by the columns used in grouping.
Groups Using an Aggregate Analytic Function
This example calculates the standard deviation, using the stddev() aggregate analytic function, for the high prices of each stock and year: