Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4ACA4B5D803 for ; Sat, 6 Aug 2011 05:54:00 -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 23393-09 for ; Sat, 6 Aug 2011 08:53:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23]) by mail.postgresql.org (Postfix) with SMTP id 5E9BFB5D800 for ; Sat, 6 Aug 2011 05:53:49 -0300 (ADT) Received: (qmail invoked by alias); 06 Aug 2011 08:53:46 -0000 Received: from a88-115-218-165.elisa-laajakaista.fi (EHLO [10.0.0.102]) [88.115.218.165] by mail.gmx.net (mp021) with SMTP; 06 Aug 2011 10:53:46 +0200 X-Authenticated: #495269 X-Provags-ID: V01U2FsdGVkX1+n/r8mQQenTkT8iN+HJUoIfQUazbgxvCdQ+zL1/X qrWems7tslRfOu Subject: Re: BUG #6146: COLLATE in ORDER BY not working with column names defined with "AS" From: Peter Eisentraut To: Matthias Kurz Cc: pgsql-bugs@postgresql.org In-Reply-To: <201108032102.p73L2gT0082970@wwwmaster.postgresql.org> References: <201108032102.p73L2gT0082970@wwwmaster.postgresql.org> Content-Type: text/plain; charset="UTF-8" Date: Sat, 06 Aug 2011 11:53:45 +0300 Message-ID: <1312620825.24721.8.camel@vanquo.pezone.net> Mime-Version: 1.0 X-Mailer: Evolution 2.32.3 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.82 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.931, T_FILL_THIS_FORM_SHORT=0.01 X-Spam-Level: X-Archive-Number: 201108/60 X-Sequence-Number: 30872 On ons, 2011-08-03 at 21:02 +0000, Matthias Kurz wrote: > 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, > --- ORDER BY can only refer to output columns by themselves, not as part of an expression. This is the same issue that SELECT a AS x FROM foo ORDER BY x works but SELECT a AS x FROM foo ORDER BY x + 1 doesn't. What is perhaps not obvious is that (a_companyName COLLATE "C") is an expression. The COLLATE clause is not specifically part of the ORDER BY syntax, but a general expression. > Is this the right behaviour? > Or a bug? Well, it works as designed and documented, and it is consistent with other behaviors, as I showed. The SQL standard is sufficiently murky on this subject, however, that I can't tell right now whether this is how it is supposed to work. But it looks like someone researched this carefully in the past, so probably yes.