Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bCpqO-00035S-EQ for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jun 2016 15:01:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bCpqO-0002gg-1E for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jun 2016 15:01:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bCppk-0001yE-N1 for pgsql-bugs@postgresql.org; Tue, 14 Jun 2016 15:00:52 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bCppg-0006i2-KH for pgsql-bugs@postgresql.org; Tue, 14 Jun 2016 15:00:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u5EF0kVE006448; Tue, 14 Jun 2016 11:00:46 -0400 From: Tom Lane 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 In-reply-to: <20160614094846.5798.5862@wrigleys.postgresql.org> References: <20160614094846.5798.5862@wrigleys.postgresql.org> Comments: In-reply-to gabrimonfa@gmail.com message dated "Tue, 14 Jun 2016 09:48:46 -0000" Date: Tue, 14 Jun 2016 11:00:46 -0400 Message-ID: <6447.1465916446@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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