SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</optional> GROUP BY ... HAVING <replaceable>boolean_expression</replaceable>
SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</optional> GROUP BY ... HAVING <replaceable>boolean_expression</replaceable>
</synopsis>
</synopsis>
Expressions in the <literal>HAVING</> clause can refer both to
Expressions in the <literal>HAVING</> clause can refer both to
grouped expressions and to ungrouped expression (which necessarily
grouped expressions and to ungrouped expressions (which necessarily
involve an aggregate function).
involve an aggregate function).
</para>
</para>
...
@@ -794,7 +794,7 @@ SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</opti
...
@@ -794,7 +794,7 @@ SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</opti
Again, a more realistic example:
Again, a more realistic example:
<programlisting>
<programlisting>
SELECT product_id, p.name, (sum(s.units) * (p.price - p.cost)) AS profit
SELECT product_id, p.name, (sum(s.units) * (p.price - p.cost)) AS profit
FROM products p LEFT JOIN sales s USING (pid)
FROM products p LEFT JOIN sales s USING (product_id)
WHERE s.date > CURRENT_DATE - INTERVAL '4 weeks'
WHERE s.date > CURRENT_DATE - INTERVAL '4 weeks'
GROUP BY product_id, p.name, p.price, p.cost
GROUP BY product_id, p.name, p.price, p.cost
HAVING sum(p.price * s.units) > 5000;
HAVING sum(p.price * s.units) > 5000;
...
@@ -1093,7 +1093,7 @@ SELECT a AS b FROM table1 ORDER BY a;
...
@@ -1093,7 +1093,7 @@ SELECT a AS b FROM table1 ORDER BY a;
<para>
<para>
If more than one sort column is specified, the later entries are
If more than one sort column is specified, the later entries are
used to sort rows that are equal under the order imposed by the
used to sort rows that are equal under the order imposed by the