|
1 | 1 | <!--
|
2 |
| -$Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.77 2003/03/25 16:15:38 petere Exp $ |
| 2 | +$Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.78 2003/06/0615:04:01 tgl Exp $ |
3 | 3 | -->
|
4 | 4 |
|
5 | 5 | <chapter id="sql-syntax">
|
@@ -1094,6 +1094,29 @@ sqrt(2)
|
1094 | 1094 | linkend="functions-aggregate">. Other aggregate functions may be added
|
1095 | 1095 | by the user.
|
1096 | 1096 | </para>
|
| 1097 | + |
| 1098 | + <para> |
| 1099 | + An aggregate expression may only appear in the result list or |
| 1100 | + <literal>HAVING</> clause of a <command>SELECT</> command. |
| 1101 | + It is forbidden in other clauses, such as <literal>WHERE</>, |
| 1102 | + because those clauses are logically evaluated before the results |
| 1103 | + of aggregates are formed. |
| 1104 | + </para> |
| 1105 | + |
| 1106 | + <para> |
| 1107 | + When an aggregate expression appears in a subquery (see |
| 1108 | + <xref linkend="sql-syntax-scalar-subqueries"> and |
| 1109 | + <xref linkend="functions-subquery">), the aggregate is normally |
| 1110 | + evaluated over the rows of the subquery. But an exception occurs |
| 1111 | + if the aggregate's argument contains only outer-level variables: |
| 1112 | + the aggregate then belongs to the nearest such outer level, and is |
| 1113 | + evaluated over the rows of that query. The aggregate expression |
| 1114 | + as a whole is then an outer reference for the subquery it appears in, |
| 1115 | + and acts as a constant over any one evaluation of that subquery. |
| 1116 | + The restriction about |
| 1117 | + appearing only in the result list or <literal>HAVING</> clause |
| 1118 | + applies with respect to the query level that the aggregate belongs to. |
| 1119 | + </para> |
1097 | 1120 | </sect2>
|
1098 | 1121 |
|
1099 | 1122 | <sect2 id="sql-syntax-type-casts">
|
@@ -1164,7 +1187,7 @@ CAST ( <replaceable>expression</replaceable> AS <replaceable>type</replaceable>
|
1164 | 1187 | </para>
|
1165 | 1188 | </sect2>
|
1166 | 1189 |
|
1167 |
| - <sect2> |
| 1190 | + <sect2 id="sql-syntax-scalar-subqueries"> |
1168 | 1191 | <title>Scalar Subqueries</title>
|
1169 | 1192 |
|
1170 | 1193 | <para>
|
|