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 1kRyWs-0000d8-6C for pgsql-general@arkaria.postgresql.org; Mon, 12 Oct 2020 14:10:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kRyWq-0001sM-0Q for pgsql-general@arkaria.postgresql.org; Mon, 12 Oct 2020 14:10:20 +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 1kRyWp-0001sF-Op for pgsql-general@lists.postgresql.org; Mon, 12 Oct 2020 14:10:19 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kRyWh-0007GL-VI for pgsql-general@postgresql.org; Mon, 12 Oct 2020 14:10:18 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 5D5133B32; Mon, 12 Oct 2020 16:10:08 +0200 (CEST) Date: Mon, 12 Oct 2020 16:10:08 +0200 From: "Peter J. Holzer" To: pgsql-general@postgresql.org Subject: Column aliases in GROUP BY and HAVING Message-ID: <20201012141008.GA25581@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="5vNYLRcllDrimb99" Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5vNYLRcllDrimb99 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In my mental model of how SQL works, the clauses of an SQL query (if present) are processed in a certain order: FROM WHERE SELECT GROUP BY HAVING ORDER BY LIMIT and each processes the output of the previous one. However, consider this: hjp=3D> select * from employees; =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A4=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=97 =E2=95=91 ssn =E2=94=82 name =E2=94=82 other_data =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=95=A2 =E2=95=91 1234010400 =E2=94=82 Alice =E2=94=82 (=E2=88=85) =E2= =95=91 =E2=95=91 2345180976 =E2=94=82 Bob =E2=94=82 (=E2=88=85) =E2= =95=91 =E2=95=91 2645101276 =E2=94=82 Carol =E2=94=82 (=E2=88=85) =E2= =95=91 =E2=95=91 9843100395 =E2=94=82 David =E2=94=82 (=E2=88=85) =E2= =95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A7=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=9D hjp=3D> select substring(ssn, 9, 2) as year, count(*) from employees gr= oup by year; =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =A4=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=97 =E2=95=91 year =E2=94=82 count =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=95=A2 =E2=95=91 95 =E2=94=82 1 =E2=95=91 =E2=95=91 76 =E2=94=82 2 =E2=95=91 =E2=95=91 00 =E2=94=82 1 =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =A7=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=9D (3 rows) In the GROUP BY clause I can use the alias year which was defined earlier in SELECT.=20 HAVING comes after GROUP BY, so I should be able to use that there, too. Right? hjp=3D> select substring(ssn, 9, 2) as year, count(*) from employees gr= oup by year having year > '20'; ERROR: column "year" does not exist LINE 1: ...ear, count(*) from employees group by year having year > '20= =2E.. Wrong. I have to use the whole expression again: hjp=3D> select substring(ssn, 9, 2) as year, count(*) from employees gr= oup by year having substring(ssn, 9, 2) > '20'; =E2=95=94=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =A4=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=97 =E2=95=91 year =E2=94=82 count =E2=95=91 =E2=95=9F=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=95=A2 =E2=95=91 95 =E2=94=82 1 =E2=95=91 =E2=95=91 76 =E2=94=82 2 =E2=95=91 =E2=95=9A=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =A7=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=9D (2 rows) This seems inconsistent to me. Is there a technical or semantic reason for this or is just "because the standard says so". hp PS: Please no discussions about the appropriateness of using an SSN as an id. This is a completely made-up example. --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --5vNYLRcllDrimb99 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl+EY7sACgkQ8g5IURL+ KF3igw//V1h07gE8gyUH+x65kdR0IxX0OphD7xnvYVyLrQ35l2kmr4Io7NWsD5W0 LvtIOFbxI8K6NIt6N70Q5I5o2kaQbUnmOCUcsj6j4jCEnv6CR9t59CseN9Kvh7CE zHgfKV2587zyCEX8aBuOpFptEOZlMLpOXS99svxBdqz11D1etEdhQk/Q/Y9WKqoi og/ZVKIwa3iMn0ywT6/jT0fy8qquWW1NWQ+kg18tFPx13qpRSjACuqd7hhe00tP4 UfA+6DlX7/kBDR3kt0bKu35iq+4p88YJ6rZx1M5xvXjC/O6h6y8HwMSFCuF9O/K5 5wSVp5X15C2SMA0iEReXteLZ6fPpN/TLKnirUAogWDae5VzNbjC+8xz5ertqivb0 x9WuVLDgn6oS8roljKVITjAeYk9ja773d/6Uk62N7ZDH6aC1FNgFvpwVkJPC5fj7 l+JtrB98nERnOhycsXNxWvTC67D6n4I/AwiGKCvyASFMXc8Zw5gpMSxe7MKt9b5B C+/LjZbt1TreMvoKRNuPbU0b5NfZUs+8cttHx04ibwM+y04jtAunpj/MryZIiq7y +0O2Kv6y2+a1iHLmDKyERuYyQ7tOnWd07hHBl6GsmzjcNXGadlZ1frh8Ge2DcQme GuzhShMieAhaE80MamgubZUR6rzw3JIp21DPjBbKBsLQYy55EP0= =n+qA -----END PGP SIGNATURE----- --5vNYLRcllDrimb99--