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 1qGKHs-0004N0-RC for pgsql-bugs@arkaria.postgresql.org; Mon, 03 Jul 2023 14:12:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qGKHr-0004Oe-Kp for pgsql-bugs@arkaria.postgresql.org; Mon, 03 Jul 2023 14:12:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qGKHr-0004MV-CB for pgsql-bugs@lists.postgresql.org; Mon, 03 Jul 2023 14:12:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qGKHo-001xlT-FD for pgsql-bugs@lists.postgresql.org; Mon, 03 Jul 2023 14:12:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 363ECBPA345995; Mon, 3 Jul 2023 10:12:12 -0400 From: Tom Lane To: hape Hape cc: pgsql-bugs@lists.postgresql.org Subject: Re: group by can use alias from select list but not the having clause In-reply-to: References: Comments: In-reply-to hape Hape message dated "Mon, 03 Jul 2023 11:43:21 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <345993.1688393531.1@sss.pgh.pa.us> Date: Mon, 03 Jul 2023 10:12:11 -0400 Message-ID: <345994.1688393531@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk hape Hape writes: > works (how does the group by know about otto if the sequence is done as > described above?) This is intentional and documented, eg https://www.postgresql.org/docs/current/sql-select.html#SQL-GROUPBY says An expression used inside a grouping_element can be an input column name, or the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. whereas the discussion of HAVING does not mention allowing output columns. The reason for this is mainly that it'd seldom be useful for a HAVING expression to be just a bare column reference, but as soon as you write something that's not a bare column reference, the output-column special case is disallowed anyway. Yes, it's messy. That's mostly because we are still trying to preserve some compatibility with SQL92, which did things differently from later standards in this area. But it's been like that for a couple of decades now, and we're not going to change it. regards, tom lane