Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 1C025B5DBDC for ; Wed, 3 Aug 2011 18:02:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 77510-09 for ; Wed, 3 Aug 2011 21:02:43 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id 73773B5DBDB for ; Wed, 3 Aug 2011 18:02:43 -0300 (ADT) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p73L2g98082971 for ; Wed, 3 Aug 2011 21:02:42 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p73L2gT0082970; Wed, 3 Aug 2011 21:02:42 GMT (envelope-from www) Date: Wed, 3 Aug 2011 21:02:42 GMT Message-Id: <201108032102.p73L2gT0082970@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #6146: COLLATE in ORDER BY not working with column names defined with "AS" From: "Matthias Kurz" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201108/34 X-Sequence-Number: 30846 The following bug has been logged online: Bug reference: 6146 Logged by: Matthias Kurz Email address: m.kurz@irregular.at PostgreSQL version: 9.1beta3 Operating system: Ubuntu 11.04 64 bit Description: COLLATE in ORDER BY not working with column names defined with "AS" Details: Running following script: --- DROP TABLE IF EXISTS myAddress; CREATE TABLE myAddress ( id SERIAL NOT NULL, companyname CHARACTER VARYING(50), street CHARACTER VARYING(50) NOT NULL, zip CHARACTER VARYING(50) NOT NULL, city CHARACTER VARYING(50) NOT NULL, country CHARACTER VARYING(50) ); INSERT INTO myAddress VALUES (DEFAULT, 'Company 2', 'Street a', '1001', 'Austria'); INSERT INTO myAddress VALUES (DEFAULT, 'Company 1', 'Street ä', '1002', 'Switzerland'); INSERT INTO myAddress VALUES (DEFAULT, 'Company 3', 'Street A', '1003', 'Italy'); INSERT INTO myAddress VALUES (DEFAULT, 'Company 4', 'Street Ä', '1004', 'France'); SELECT a.companyname AS a_companyname, a.street, a.zip, a.city, a.country FROM myAddress a ORDER BY a_companyName COLLATE "C" DESC, a.street COLLATE "C" ASC, a.zip COLLATE "C" ASC, a.city COLLATE "C" ASC, a.country COLLATE "C" ASC --- Gives me following error: --- ERROR: column "a_companyname" does not exist LINE 26: a_companyName COLLATE "C" ASC, --- When removing the COLLATE "C" for line 26 (in this line only! the others are still present), it works. After this change I tried different things e.g. changed ASC to DESC or used "en_GB.utf8" instead of "C" collate. Everything worked as expected. So the COLLATE statement is honored in the ORDER BY clause except when using it on a column which was defined explicitly with an "AS" statement. Is this the right behaviour? Or a bug? Greetings, Matthias