doc: use FILTER in aggregate example
authorBruce Momjian <[email protected]>
Thu, 1 Sep 2022 02:19:06 +0000 (22:19 -0400)
committerBruce Momjian <[email protected]>
Thu, 1 Sep 2022 02:19:06 +0000 (22:19 -0400)
Reported-by: [email protected]
Discussion: https://postgr.es/m/163499710897.684.7420075366995883688@wrigleys.postgresql.org

Back-through: 10

doc/src/sgml/query.sgml

index 6f31a5a1d621c0cbe94892ca8c7f37a58a9399bd..8243bf51af8c38139f1a0ed06d6ec71c8f5da058 100644 (file)
@@ -726,19 +726,20 @@ SELECT city, max(temp_lo)
     which gives us one output row per city.  Each aggregate result is
     computed over the table rows matching that city.
     We can filter these grouped
-    rows using <literal>HAVING</literal>:
+    rows using <literal>HAVING</literal> and the output count using
+    <literal>FILTER</literal>:
 
 <programlisting>
-SELECT city, max(temp_lo)
+SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo &lt; 30)
     FROM weather
     GROUP BY city
     HAVING max(temp_lo) &lt; 40;
 </programlisting>
 
 <screen>
-  city   | max
----------+-----
- Hayward |  37
+  city   | max | count
+---------+-----+-------
+ Hayward |  37 |     5
 (1 row)
 </screen>
 
@@ -748,7 +749,7 @@ SELECT city, max(temp_lo)
     names begin with <quote><literal>S</literal></quote>, we might do:
 
 <programlisting>
-SELECT city, max(temp_lo)
+SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo &lt; 30)
     FROM weather
     WHERE city LIKE 'S%'            -- <co id="co.tutorial-agg-like"/>
     GROUP BY city