Re: Is it possible to make the order of output the same as the order of input parameters?

From: Stephen Frost <sfrost(at)snowman(dot)net>
To: "m(dot) hvostinski" <makhvost(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is it possible to make the order of output the same as the order of input parameters?
Date: 2010-06-02 11:43:36
Message-ID: 20100602114336.GC21875@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

* m. hvostinski (makhvost(at)gmail(dot)com) wrote:
> I have a simple query like:
>
> SELECT * FROM customer WHERE id IN (23, 56, 2, 12, 10)
>
> The problem is that I need to retrieve the rows in the same order as the set
> of ids provided in the select statement. Can it be done?

Not very easily. My first thought would be doing something like:

SELECT
customer.*
FROM
customer a
JOIN (VALUES (1,23),(2,56),(3,2),(4,12),(5,10)) b
ON (a.id = b.column2)
ORDER BY b.column1
;

Thanks,

Stephen

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Kretschmer 2010-06-02 11:45:21 Re: Is it possible to make the order of output the same as the order of input parameters?
Previous Message Stephen Frost 2010-06-02 11:41:10 Re: Out of Memory and Configuration Problems (Big Computer)