From 595ed2a8550e34c0abe64569a104d92ad077ec08 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 10 Mar 2005 23:21:26 +0000 Subject: Make the behavior of HAVING without GROUP BY conform to the SQL spec. Formerly, if such a clause contained no aggregate functions we mistakenly treated it as equivalent to WHERE. Per spec it must cause the query to be treated as a grouped query of a single group, the same as appearance of aggregate functions would do. Also, the HAVING filter must execute after aggregate function computation even if it itself contains no aggregate functions. --- doc/src/sgml/query.sgml | 7 ++++--- doc/src/sgml/ref/select.sgml | 13 ++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/query.sgml b/doc/src/sgml/query.sgml index 366820e5546..e573db56cb6 100644 --- a/doc/src/sgml/query.sgml +++ b/doc/src/sgml/query.sgml @@ -1,5 +1,5 @@ @@ -783,8 +783,9 @@ SELECT city, max(temp_lo) will be inputs to the aggregates. On the other hand, the HAVING clause always contains aggregate functions. (Strictly speaking, you are allowed to write a HAVING - clause that doesn't use aggregates, but it's wasteful. The same condition - could be used more efficiently at the WHERE stage.) + clause that doesn't use aggregates, but it's seldom useful. The same + condition could be used more efficiently at the WHERE + stage.) diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 855412c36c9..93218e16c2e 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1,5 +1,5 @@ @@ -452,6 +452,17 @@ HAVING condition unambiguously reference a grouping column, unless the reference appears within an aggregate function. + + + The presence of HAVING turns a query into a grouped + query even if there is no GROUP BY clause. This is the + same as what happens when the query contains aggregate functions but + no GROUP BY clause. All the selected rows are considered to + form a single group, and the SELECT list and + HAVING clause can only reference table columns from + within aggregate functions. Such a query will emit a single row if the + HAVING condition is true, zero rows if it is not true. + -- cgit v1.2.3