doc: use FILTER in aggregate example
Reported-by: michal.palenik@freemap.sk Discussion: https://postgr.es/m/163499710897.684.7420075366995883688@wrigleys.postgresql.org Backpatch-through: 10
This commit is contained in:
parent
fbd597e0b8
commit
fede154172
@ -726,19 +726,20 @@ SELECT city, max(temp_lo)
|
|||||||
which gives us one output row per city. Each aggregate result is
|
which gives us one output row per city. Each aggregate result is
|
||||||
computed over the table rows matching that city.
|
computed over the table rows matching that city.
|
||||||
We can filter these grouped
|
We can filter these grouped
|
||||||
rows using <literal>HAVING</literal>:
|
rows using <literal>HAVING</literal> and the output count using
|
||||||
|
<literal>FILTER</literal>:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
SELECT city, max(temp_lo)
|
SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo < 30)
|
||||||
FROM weather
|
FROM weather
|
||||||
GROUP BY city
|
GROUP BY city
|
||||||
HAVING max(temp_lo) < 40;
|
HAVING max(temp_lo) < 40;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
<screen>
|
<screen>
|
||||||
city | max
|
city | max | count
|
||||||
---------+-----
|
---------+-----+-------
|
||||||
Hayward | 37
|
Hayward | 37 | 5
|
||||||
(1 row)
|
(1 row)
|
||||||
</screen>
|
</screen>
|
||||||
|
|
||||||
@ -748,7 +749,7 @@ SELECT city, max(temp_lo)
|
|||||||
names begin with <quote><literal>S</literal></quote>, we might do:
|
names begin with <quote><literal>S</literal></quote>, we might do:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
SELECT city, max(temp_lo)
|
SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo < 30)
|
||||||
FROM weather
|
FROM weather
|
||||||
WHERE city LIKE 'S%' -- <co id="co.tutorial-agg-like"/>
|
WHERE city LIKE 'S%' -- <co id="co.tutorial-agg-like"/>
|
||||||
GROUP BY city
|
GROUP BY city
|
||||||
|
Loading…
x
Reference in New Issue
Block a user