Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 7890D9FB856 for ; Thu, 15 Feb 2007 10:04:16 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 41958-02 for ; Thu, 15 Feb 2007 10:04:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from proton.planetargon.com (proton.planetargon.com [198.145.37.35]) by postgresql.org (Postfix) with ESMTP id 461759FB708 for ; Thu, 15 Feb 2007 10:04:09 -0400 (AST) Received: from [172.16.1.106] (unknown [61.197.227.146]) by proton.planetargon.com (Postfix) with ESMTP id 054AF872284; Thu, 15 Feb 2007 06:04:06 -0800 (PST) In-Reply-To: <45D46196.2090003@archonet.com> References: <32cabba0702150319v427d5a25s9a6c7f851576baa2@mail.gmail.com> <45D46196.2090003@archonet.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <57FD2B24-DB43-4EF7-92D1-3C467641AF9D@seespotcode.net> Cc: Walter Cruz , pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: "for SELECT DISTINCT, ORDER BY expressions must appear in select list" - is that the standart or a limitation of postgresql? Date: Thu, 15 Feb 2007 23:04:04 +0900 To: Richard Huxton X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.052 tagged_above=0 required=5 tests=AWL, BAYES_50 X-Spam-Level: * X-Archive-Number: 200702/164 X-Sequence-Number: 27498 On Feb 15, 2007, at 22:35 , Richard Huxton wrote: > Walter Cruz wrote: >> SELECT distinct name from test order by number >> (well, I think that que query doesn't make any sense, but raises >> the error :) ) >> The error: ERROR: for SELECT DISTINCT, ORDER BY expressions must >> appear in select list is due to a standart implementarion or a design >> decision of postgres? > > I think ORDER BY is defined to take place after DISTINCT, which > means there is no meaningful "number" for it to order by. You could > arbitrarily choose the first number encountered, but I can't see > what sense it would make to order by them. I believe the reason is that DISTINCT depends on a sort to determine uniqueness (distinctness), so it's a implementation detail that if you're going to include an ORDER BY, you also need to include the same columns in the ORDER BY in the DISTINCT clause. Though I suspect Richard is right that ORDER BY takes place after DISTINCT. (My cursory attempt at parsing the SQL 2003 draft failed me.) On further thought, I bet SELECT DISTINCT name FROM test ORDER BY name, number fails with a different error, one directly supporting Richard's conclusion. Michael Glaesemann grzm seespotcode net