pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: gabrimonfa@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
Date: Tue, 14 Jun 2016 11:00:46 -0400
Message-ID: <6447.1465916446@sss.pgh.pa.us> (raw)
In-Reply-To: <20160614094846.5798.5862@wrigleys.postgresql.org>
References: <20160614094846.5798.5862@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

gabrimonfa@gmail.com writes:
> CREATE TABLE table1 (id integer primary key, name varchar);
> CREATE TABLE table2 (id integer primary key, home varchar);

> SELECT DISTINCT name FROM table1 t1 INNER JOIN table2 t2 ON (t1.id = t2.id)
> ORDER BY t1.name;
> ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
> SELECT DISTINCT name FROM table1 t1 INNER JOIN table2 t2 ON (t1.id = t2.id)
> ORDER BY name;
> [ok]

The reason for the discrepancy is that "t1.name" refers to an output
column of t1, while "name" refers to an output column of the unnamed JOIN.
While those are semantically equivalent in this particular case, they are
not so in general --- in particular, had this been a FULL JOIN, they
would definitely not be equivalent.  PG's parser treats them as different
variables and therefore sees "ORDER BY t1.name" as unrelated to the value
being distinct'ed on.

We might someday try to make the parser smarter about recognizing such
equivalences earlier, but I'm not terribly excited about it.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (3+ messages)  latest in thread

Message-ID: <6447.1465916446@sss.pgh.pa.us>
Permalink:  ../6447.1465916446@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/6447.1465916446@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 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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, gabrimonfa@gmail.com
  Subject: Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
  In-Reply-To: <6447.1465916446@sss.pgh.pa.us>
  References: <20160614094846.5798.5862@wrigleys.postgresql.org> <6447.1465916446@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