pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dawid Kuroczko <qnex42@gmail.com>
To: Riya Verghese <riya.verghese@admissioncorp.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: ORDER records based on parameters in IN clause
Date: Wed, 29 Jun 2005 13:44:00 +0200
Message-ID: <758d5e7f05062904445112c493@mail.gmail.com> (raw)
In-Reply-To: <BBA4F47BB0CEDD4D8ADD2FE21E2823673110A4@dhost002-37.dex002.intermedia.net>
References: <BBA4F47BB0CEDD4D8ADD2FE21E2823673110A4@dhost002-37.dex002.intermedia.net>

On 6/27/05, Riya Verghese <riya.verghese@admissioncorp.com> wrote:
> I have a stmt where the outer-query is limited by the results of the inner
> query. I would like the outer query to return records in the same order as
> the values provided in the IN clause (returned form the inner query). 
> 
> The inner_query is returning id's ordered by count(id) , i.e by most common
> occurrence. 
> 
> In essence, 
> 
> when I say 
> 
> select * from table where id IN (2003,1342,799, 1450) 
> 
> Currently postgres returns it in this order (1450,1342,799,2003) 

Simplest, though not niciest solution would be:

SELECT * FROM table WHERE id IN (2003,1342,799,1450) ORDER BY id =
2003 DESC, id = 1342 DESC, id = 799 DESC, id = 1450 DESC;

You could write a function which will return position of interger
inside integer[] array and use it as order key. :-)

   Regards,
       Dawid



view thread (16+ messages)  latest in thread

Message-ID: <758d5e7f05062904445112c493@mail.gmail.com>
Permalink:  ../758d5e7f05062904445112c493@mail.gmail.com/
Also on:    postgresql.org/message-id/758d5e7f05062904445112c493@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: qnex42@gmail.com, riya.verghese@admissioncorp.com
  Subject: Re: ORDER records based on parameters in IN clause
  In-Reply-To: <758d5e7f05062904445112c493@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox