Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOLmD-0000nQ-Rf for pgsql-docs@arkaria.postgresql.org; Wed, 17 Aug 2022 16:20:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oOLmC-0003Ft-1Z for pgsql-docs@arkaria.postgresql.org; Wed, 17 Aug 2022 16:20:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOLmB-0003Fk-Q8 for pgsql-docs@lists.postgresql.org; Wed, 17 Aug 2022 16:20:15 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOLm9-0005t5-Ak for pgsql-docs@lists.postgresql.org; Wed, 17 Aug 2022 16:20:14 +0000 Received: from bruce by momjian.us with local (Exim 4.94.2) (envelope-from ) id 1oOLm6-001D5X-Pg; Wed, 17 Aug 2022 12:20:10 -0400 Date: Wed, 17 Aug 2022 12:20:10 -0400 From: Bruce Momjian To: michal.palenik@freemap.sk, pgsql-docs@lists.postgresql.org Subject: Re: nicer examples for aggregate calls Message-ID: References: <163499710897.684.7420075366995883688@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="XkMLcYuso1tTC2OC" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <163499710897.684.7420075366995883688@wrigleys.postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --XkMLcYuso1tTC2OC Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Sat, Oct 23, 2021 at 01:51:48PM +0000, PG Doc comments form wrote: > The following documentation comment has been logged on the website: > > Page: https://www.postgresql.org/docs/14/tutorial-agg.html > Description: > > currently, all of the examples are very simple, like > > SELECT city, max(temp_lo) > FROM weather > WHERE city LIKE 'S%' -- (1) > GROUP BY city > HAVING max(temp_lo) < 40; > > this example would be more complex and would allow users to search for > clause "filter": > > Finally, if we only care about cities whose names begin with ā€œSā€ and we want > to calculate the number of observations in each city with temp_lo over 30, > we might do: > > SELECT city, max(temp_lo), count(*) filter (temp_lo>30), > FROM weather > WHERE city LIKE 'S%' -- (1) > GROUP BY city > HAVING max(temp_lo) < 40; Good idea. We didn't support FILTER at the time this query was added. Here is a patch which adds it. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Indecision is a decision. Inaction is an action. Mark Batterson --XkMLcYuso1tTC2OC Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="filter.diff" diff --git a/doc/src/sgml/query.sgml b/doc/src/sgml/query.sgml index 6f31a5a1d6..8243bf51af 100644 --- a/doc/src/sgml/query.sgml +++ b/doc/src/sgml/query.sgml @@ -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 HAVING: + rows using HAVING and the output count using + FILTER: -SELECT city, max(temp_lo) +SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo < 30) FROM weather GROUP BY city HAVING max(temp_lo) < 40; - city | max ----------+----- - Hayward | 37 + city | max | count +---------+-----+------- + Hayward | 37 | 5 (1 row) @@ -748,7 +749,7 @@ SELECT city, max(temp_lo) names begin with S, we might do: -SELECT city, max(temp_lo) +SELECT city, max(temp_lo), count(*) FILTER (WHERE temp_lo < 30) FROM weather WHERE city LIKE 'S%' -- GROUP BY city --XkMLcYuso1tTC2OC--