@@ -109,9 +109,11 @@ TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ]
109
109
110
110
<listitem>
111
111
<para>
112
- If the <literal>GROUP BY</literal> clause is specified, the
112
+ If the <literal>GROUP BY</literal> clause is specified,
113
+ or if there are aggregate function calls, the
113
114
output is combined into groups of rows that match on one or more
114
- values. If the <literal>HAVING</literal> clause is present, it
115
+ values, and the results of aggregate functions are computed.
116
+ If the <literal>HAVING</literal> clause is present, it
115
117
eliminates groups that do not satisfy the given condition. (See
116
118
<xref linkend="sql-groupby" endterm="sql-groupby-title"> and
117
119
<xref linkend="sql-having" endterm="sql-having-title"> below.)
@@ -637,24 +639,37 @@ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...]
637
639
638
640
<para>
639
641
Aggregate functions, if any are used, are computed across all rows
640
- making up each group, producing a separate value for each group
641
- (whereas without <literal>GROUP BY</literal>, an aggregate
642
- produces a single value computed across all the selected rows).
643
- The set of rows fed to the aggregate function can be further filtered by
642
+ making up each group, producing a separate value for each group.
643
+ (If there are aggregate functions but no <literal>GROUP BY</literal>
644
+ clause, the query is treated as having a single group comprising all
645
+ the selected rows.)
646
+ The set of rows fed to each aggregate function can be further filtered by
644
647
attaching a <literal>FILTER</literal> clause to the aggregate function
645
648
call; see <xref linkend="syntax-aggregates"> for more information. When
646
649
a <literal>FILTER</literal> clause is present, only those rows matching it
647
- are included.
648
- When <literal>GROUP BY</literal> is present, it is not valid for
650
+ are included in the input to that aggregate function.
651
+ </para>
652
+
653
+ <para>
654
+ When <literal>GROUP BY</literal> is present,
655
+ or any aggregate functions are present, it is not valid for
649
656
the <command>SELECT</command> list expressions to refer to
650
- ungrouped columns except within aggregate functions or if the
657
+ ungrouped columns except within aggregate functions or when the
651
658
ungrouped column is functionally dependent on the grouped columns,
652
659
since there would otherwise be more than one possible value to
653
660
return for an ungrouped column. A functional dependency exists if
654
661
the grouped columns (or a subset thereof) are the primary key of
655
662
the table containing the ungrouped column.
656
663
</para>
657
664
665
+ <para>
666
+ Keep in mind that all aggregate functions are evaluated before
667
+ evaluating any <quote>scalar</> expressions in the <literal>HAVING</>
668
+ clause or <literal>SELECT</> list. This means that, for example,
669
+ a <literal>CASE</> expression cannot be used to skip evaluation of
670
+ an aggregate function; see <xref linkend="syntax-express-eval">.
671
+ </para>
672
+
658
673
<para>
659
674
Currently, <literal>FOR NO KEY UPDATE</>, <literal>FOR UPDATE</>,
660
675
<literal>FOR SHARE</> and <literal>FOR KEY SHARE</> cannot be
@@ -683,7 +698,8 @@ HAVING <replaceable class="parameter">condition</replaceable>
683
698
created by <literal>GROUP BY</literal>. Each column referenced in
684
699
<replaceable class="parameter">condition</replaceable> must
685
700
unambiguously reference a grouping column, unless the reference
686
- appears within an aggregate function.
701
+ appears within an aggregate function or the ungrouped column is
702
+ functionally dependent on the grouping columns.
687
703
</para>
688
704
689
705
<para>
0 commit comments