pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: ivningman@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18542: Order by expression, that contains column from projection isn't working
Date: Tue, 16 Jul 2024 08:45:15 -0400
Message-ID: <3319082.1721133915@sss.pgh.pa.us> (raw)
In-Reply-To: <18542-adf5273ccfea5558@postgresql.org>
References: <18542-adf5273ccfea5558@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> writes:
> select z as y from
>  (select 1 as z) x
> order by y + 1
> That query finishes with error: 
> ERROR: column "y" does not exist
>   Position: 48
> But wrapping it with select works fine:
> select * from (select z as y from
>  (select 1 as z) x) c
> order by y + 1

This is not a bug.  See

https://www.postgresql.org/docs/current/queries-order.html

particularly "Note that an output column name has to stand alone, that
is, it cannot be used in an expression".  The fact that we allow this
at all is a hangover from SQL92; per SQL99 and later, variables in
ORDER BY should refer to input columns (that is, outputs of the FROM
clause).

			regards, tom lane





view thread (2+ messages)

Message-ID: <3319082.1721133915@sss.pgh.pa.us>
Permalink:  ../3319082.1721133915@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3319082.1721133915@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ivningman@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18542: Order by expression, that contains column from projection isn't working
  In-Reply-To: <3319082.1721133915@sss.pgh.pa.us>

* 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