From e649796f128bd8702ba5744d36f4e8cb81f0b754 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 6 Jun 2003 15:04:03 +0000 Subject: Implement outer-level aggregates to conform to the SQL spec, with extensions to support our historical behavior. An aggregate belongs to the closest query level of any of the variables in its argument, or the current query level if there are no variables (e.g., COUNT(*)). The implementation involves adding an agglevelsup field to Aggref, and treating outer aggregates like outer variables at planning time. --- doc/src/sgml/syntax.sgml | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml index b6ebb1cc434..a1c0767e4cc 100644 --- a/doc/src/sgml/syntax.sgml +++ b/doc/src/sgml/syntax.sgml @@ -1,5 +1,5 @@ @@ -1094,6 +1094,29 @@ sqrt(2) linkend="functions-aggregate">. Other aggregate functions may be added by the user. + + + An aggregate expression may only appear in the result list or + HAVING clause of a SELECT command. + It is forbidden in other clauses, such as WHERE, + because those clauses are logically evaluated before the results + of aggregates are formed. + + + + When an aggregate expression appears in a subquery (see + and + ), the aggregate is normally + evaluated over the rows of the subquery. But an exception occurs + if the aggregate's argument contains only outer-level variables: + the aggregate then belongs to the nearest such outer level, and is + evaluated over the rows of that query. The aggregate expression + as a whole is then an outer reference for the subquery it appears in, + and acts as a constant over any one evaluation of that subquery. + The restriction about + appearing only in the result list or HAVING clause + applies with respect to the query level that the aggregate belongs to. + @@ -1164,7 +1187,7 @@ CAST ( expression AS type - + Scalar Subqueries -- cgit v1.2.3