Re: BUG #18542: Order by expression, that contains column from projection isn't working

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: ivningman(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18542: Order by expression, that contains column from projection isn't working
Date: 2024-07-16 12:45:15
Message-ID: 3319082.1721133915@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2024-07-17 12:53:39 BUG #18543: Mistake in docs example
Previous Message Etsuro Fujita 2024-07-16 10:29:16 Re: BUG #17889: Invalid cursor direction for a foreign scan that reached the fetch_size (MOVE BACKWARD ALL IN cX)