Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwYq9-0005Mf-5J for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Dec 2014 16:01:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XwYq8-0003at-4d for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Dec 2014 16:01:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XwYq7-0003ah-3x for pgsql-bugs@postgresql.org; Thu, 04 Dec 2014 16:01:11 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwYpz-0003Bd-04 for pgsql-bugs@postgresql.org; Thu, 04 Dec 2014 16:01:09 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id 79655C9DA6C for ; Thu, 4 Dec 2014 08:01:02 -0800 (PST) Date: Thu, 4 Dec 2014 09:01:02 -0700 (MST) From: David G Johnston To: pgsql-bugs@postgresql.org Message-ID: <1417708862460-5829256.post@n5.nabble.com> In-Reply-To: <20141204064346.2573.21378@wrigleys.postgresql.org> References: <20141204064346.2573.21378@wrigleys.postgresql.org> Subject: Re: BUG #12137: ORDER BY with expresion changes the output if added MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.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 dima-2 wrote > FROM "geonames" LEFT OUTER JOIN geoname_names > ON geoname_names.geoname_id = geonames.geoname_id > AND geoname_names.name_id IN > (SELECT name_id FROM geoname_names > WHERE geoname_names.geoname_id = geonames.geoname_id > AND geoname_names.language = 'ru' > ORDER BY geoname_names.short DESC, geoname_names.preffered DESC, > geoname_names.colloquial, geoname_names.historic > LIMIT 1 1. This could use aliases since you have repeating table names 2. I would write the above portion as: geonames LEFT JOIN ( SELDCT DISTINCT ON name_id, geoname_id FROM ... WHERE ... ORDER BY ... ) gnn USING (geoname_id) 3. I would supply a query of the form: WITH geonames AS ( values (...),(...) ) , geoname_names AS ( values (...) ) And then show what you want the table output of the query to be. David J. -- View this message in context: http://postgresql.nabble.com/BUG-12137-ORDER-BY-with-expresion-changes-the-output-if-added-tp5829220p5829256.html Sent from the PostgreSQL - bugs mailing list archive at Nabble.com. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs