pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@postgresql.org
Subject: Column aliases in GROUP BY and HAVING
Date: Mon, 12 Oct 2020 16:10:08 +0200
Message-ID: <20201012141008.GA25581@hjp.at> (raw)

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=> select * from employees;
    ╔════════════╤═══════╤════════════╗
    ║    ssn     │ name  │ other_data ║
    ╟────────────┼───────┼────────────╢
    ║ 1234010400 │ Alice │ (∅)        ║
    ║ 2345180976 │ Bob   │ (∅)        ║
    ║ 2645101276 │ Carol │ (∅)        ║
    ║ 9843100395 │ David │ (∅)        ║
    ╚════════════╧═══════╧════════════╝

    hjp=> select substring(ssn, 9, 2) as year, count(*) from employees group by year;
    ╔══════╤═══════╗
    ║ year │ count ║
    ╟──────┼───────╢
    ║ 95   │     1 ║
    ║ 76   │     2 ║
    ║ 00   │     1 ║
    ╚══════╧═══════╝
    (3 rows)

In the GROUP BY clause I can use the alias year which was defined
earlier in SELECT. 

HAVING comes after GROUP BY, so I should be able to use that there, too.
Right?

    hjp=> select substring(ssn, 9, 2) as year, count(*) from employees group by year having year > '20';
    ERROR:  column "year" does not exist
    LINE 1: ...ear, count(*) from employees group by year having year > '20...

Wrong. I have to use the whole expression again:

    hjp=> select substring(ssn, 9, 2) as year, count(*) from employees group by year having substring(ssn, 9, 2) > '20';
    ╔══════╤═══════╗
    ║ year │ count ║
    ╟──────┼───────╢
    ║ 95   │     1 ║
    ║ 76   │     2 ║
    ╚══════╧═══════╝
    (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.

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (3+ messages)  latest in thread

Message-ID: <20201012141008.GA25581@hjp.at>
Permalink:  ../20201012141008.GA25581@hjp.at/
Also on:    postgresql.org/message-id/20201012141008.GA25581@hjp.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: Column aliases in GROUP BY and HAVING
  In-Reply-To: <20201012141008.GA25581@hjp.at>
  References: <20201012141008.GA25581@hjp.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox