X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A5B76529EF for ; Wed, 29 Jun 2005 09:10:41 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 39342-08 for ; Wed, 29 Jun 2005 12:10:37 +0000 (GMT) Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 9EC4D529E4 for ; Wed, 29 Jun 2005 09:10:36 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id B59DB30B41; Wed, 29 Jun 2005 14:13:05 +0200 (MET DST) From: Zac X-Newsgroups: pgsql.sql Subject: Re: ORDER records based on parameters in IN clause Date: Wed, 29 Jun 2005 14:09:35 +0200 Organization: Hub.Org Networking Services Lines: 18 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en In-Reply-To: To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.041 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/351 X-Sequence-Number: 21952 Riya Verghese wrote: > select * from table where id IN (2003,1342,799, 1450) > > I would like the records to be ordered as 2003, 1342, 799, 1450. The > outer query has no knowledge of the count(id) that the inner_query is > ordering by. I think this is the real problem: outer query must know count(id) to order by count(id). You can use it in the outer with something like this: SELECT table.* FROM table JOIN (SELECT id, count(id) AS count FROM... your subquery) AS x ORDER BY x.count Bye.