This section uses the ROLLUP sub-clause of the GROUP BY clause in the explanation and example.
Consider a single-table query of the following form:
SELECT select-list FROM table WHERE where-search-condition GROUP BY [ group-by-expression | ROLLUP (group-by-expression) ] HAVING having-search-condition
This query is executed in the following manner:
Apply the WHERE clause This generates an intermediate result that contains only some of the rows of the table.
Partition the result into groups This action generates an intermediate result with one row for each group as dictated by the GROUP BY clause. Each generated row contains the group-by-expression for each group, and the computed aggregate functions in the select-list and having-search-condition.
Apply the HAVING clause Any rows from this second intermediate result that do not meet the criteria of the HAVING clause are removed at this point.
Project out the results to display This action takes from step 3 only those columns that need to be displayed in the result set of the query—that is, it takes only those columns corresponding to the expressions from the select-list.
This process makes requirements on queries with a GROUP BY clause:
The WHERE clause is evaluated first. Therefore, any aggregate functions are evaluated only over those rows that satisfy the WHERE clause.
The final result set is built from the second intermediate result, which holds the partitioned rows. The second intermediate result holds rows corresponding to the group-by-expression. Therefore, if an expression that is not an aggregate function appears in the select-list, then it must also appear in the group-by-expression. No function evaluation can be performed during the projection step.
An expression can be included in the group-by-expression but not in the select-list. It is projected out in the result.
Discuss this page in DocCommentXchange.
|Copyright © 2014, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0|