Re: ORDER records based on parameters in IN clause

From: Dawid Kuroczko <qnex42(at)gmail(dot)com>
To: Riya Verghese <riya(dot)verghese(at)admissioncorp(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ORDER records based on parameters in IN clause
Date: 2005-06-29 11:44:00
Message-ID: 758d5e7f05062904445112c493@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 6/27/05, Riya Verghese <riya(dot)verghese(at)admissioncorp(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2005-06-29 11:57:23 Re: ORDER records based on parameters in IN clause
Previous Message Russell Simpkins 2005-06-29 11:19:22 Re: ORDER records based on parameters in IN clause