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 C0E6F529F3 for ; Wed, 29 Jun 2005 08:44:03 -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 33131-04 for ; Wed, 29 Jun 2005 11:43:58 +0000 (GMT) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.204]) by svr1.postgresql.org (Postfix) with ESMTP id B0475528C1 for ; Wed, 29 Jun 2005 08:43:56 -0300 (ADT) Received: by zproxy.gmail.com with SMTP id 8so466570nzo for ; Wed, 29 Jun 2005 04:44:00 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=QM/gKf1ra4659IFXJJMJARhksQayLS8M3ew0MIe02SFliW1lwztzWlobFFEXHoXxc2J7YI94vjzaHjjsP8NshhNUZZI5XrhcvngpjLOT196R9Th1aqPZm4SCQwbVsUS23RQ+UQsl7f0evNR2r/dPTTkq+qMMNl3aKfiZdv4NnlY= Received: by 10.36.4.14 with SMTP id 14mr2738249nzd; Wed, 29 Jun 2005 04:44:00 -0700 (PDT) Received: by 10.36.22.20 with HTTP; Wed, 29 Jun 2005 04:44:00 -0700 (PDT) Message-ID: <758d5e7f05062904445112c493@mail.gmail.com> Date: Wed, 29 Jun 2005 13:44:00 +0200 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: Riya Verghese Subject: Re: ORDER records based on parameters in IN clause Cc: pgsql-sql@postgresql.org In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.625 tagged_above=0 required=5 tests=AWL, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200506/349 X-Sequence-Number: 21950 On 6/27/05, Riya Verghese wrote: > I have a stmt where the outer-query is limited by the results of the inne= r > query. I would like the outer query to return records in the same order a= s > the values provided in the IN clause (returned form the inner query).=20 >=20 > The inner_query is returning id's ordered by count(id) , i.e by most comm= on > occurrence.=20 >=20 > In essence,=20 >=20 > when I say=20 >=20 > select * from table where id IN (2003,1342,799, 1450)=20 >=20 > Currently postgres returns it in this order (1450,1342,799,2003)=20 Simplest, though not niciest solution would be: SELECT * FROM table WHERE id IN (2003,1342,799,1450) ORDER BY id =3D 2003 DESC, id =3D 1342 DESC, id =3D 799 DESC, id =3D 1450 DESC; You could write a function which will return position of interger inside integer[] array and use it as order key. :-) Regards, Dawid