Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qe2JE-005YTL-FC for pgsql-docs@arkaria.postgresql.org; Wed, 06 Sep 2023 23:51:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qe2JD-00Dv0x-9R for pgsql-docs@arkaria.postgresql.org; Wed, 06 Sep 2023 23:51:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qe2JC-00Dv0g-SS for pgsql-docs@lists.postgresql.org; Wed, 06 Sep 2023 23:51:42 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qe2J8-003Vov-Kv for pgsql-docs@lists.postgresql.org; Wed, 06 Sep 2023 23:51:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=ob2zdDwHAqUbQ5HPFuxjwTc5NPoVJkzKo/+8MNynh7Q=; b=kTk3WSJUn12btmEp1zyzQ09Ipn 6zwsfoz35Eo/ng5YQaDfx2MvRH+JJkOVMStX1bBqhuhtxb+l9xsLLtokmyhEIOjyCGHhJ16RpQB0l SGxAcJp4WprXkgXzWV9Czk8aB/fsPdU0UMe0OP7bU+0u41+fCe+iC3JX3onmPm1QOZo5CKrNPPZ3u Vm3lbDtZiEfw8yGKD2Hz5IXX43kOfqTJL2eAP5CCI8+N0K/U6IArsTbwLnQfoShUkXZPYCbGCITyZ WSeDIogdgiHR9rTksZORBrAaxBtjsNWlV2XwJKDU3I89HR/5Ba/gvBSesk+uWy/Bluugi+yWtCRQo 6y4OD/wg==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1qe2J4-005lCt-2j; Wed, 06 Sep 2023 19:51:34 -0400 Date: Wed, 6 Sep 2023 19:51:34 -0400 From: Bruce Momjian To: "David G. Johnston" Cc: Laurenz Albe , postgres-hape@gmx.de, pgsql-docs@lists.postgresql.org Subject: Re: group by can use alias from select list Message-ID: References: <168871536004.379168.9352636188330923805@wrigleys.postgresql.org> <25b70e6ec7bef81acac508dc61dfa01e72c96252.camel@cybertec.at> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="pUmxj/d3rwaUVJ14" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --pUmxj/d3rwaUVJ14 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Jul 11, 2023 at 07:31:26AM -0700, David G. Johnston wrote: > On Tue, Jul 11, 2023 at 7:19 AM Laurenz Albe wrote: > > On Fri, 2023-07-07 at 07:36 +0000, PG Doc comments form wrote: > > i have a discussion in pgsql-bugs@lists.postgresql.org about this and it > is > > mentioned in the answers that it is documented and a "feature" of > Postgres > > that this can be done. > > If this is wanted the documentation > > (https://www.postgresql.org/docs/current/sql-select.html) that mentioned: > > [execution order of SELECT that says that GROUP BY is before SELECT] > > isn't correct because how  can 4.) be done and the alias from 5.) is > used? > > Here is a hint important that there is an exception for alias used in > group > > by > > I think that is already documented: > > > > I think the complaint is that someone seeing the behavior in the wild comes to > this order-of-operations and doesn't see that the observed behavior is > documented.  Sure, they can go into the GROUP BY section and figure out that > there is a "oh, by the way" comment within there that output columns/aliases > are indeed allowed.  But I tend to agree that a mention in the "order of > operations" section that output columns from step 5 can be seen in step 4 seems > like an improvement if we really want the order of operations to be the main > reference entry point for people trying to work out query behavior.  Something > like:  "(while unadvised it is possible for the aliases defined in the next > step to be used here as well)". How is the attached patch? -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you. --pUmxj/d3rwaUVJ14 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="group-by.diff" diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 0ee0cc7e64..9c354f7033 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -130,7 +130,9 @@ TABLE [ ONLY ] table_name [ * ] If the HAVING clause is present, it eliminates groups that do not satisfy the given condition. (See and - below.) + below.) Names and ordinal numbers of + output columns, computed in the next step, can also be referenced + by GROUP BY. --pUmxj/d3rwaUVJ14--