Re: Selecting rows with "static" ordering

From: "Aaron Bono" <postgresql(at)aranya(dot)com>
To: "Steve Midgley" <public(at)misuse(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting rows with "static" ordering
Date: 2007-04-27 00:46:58
Message-ID: bf05e51c0704261746m3c6cea8dk7003e3806fd457ca@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 4/26/07, Steve Midgley <public(at)misuse(dot)org> wrote:
>
> So take
> for example this foreign application variable:
>
> ids = "3,2,5,1,4"
>
> The application then executes this sql:
>
> select * from table where id in (3,2,5,1,4)
>
> As-is, of course, the above query will return the 5 records in a
> semi-random (i.e. unpredictable/unreliable) order. And I don't want to
> just "order by id" - I want to "order by id(3,2,5,1,4)" (if you see
> what I mean)
>
>
select *
from table
where id in (3, 2, 5, 1, 4)
order by
case when (id = 3) then 1
when (id = 2) then 2
when (id = 5) then 3
when (id = 1) then 4
when (id = 4) then 5
else null
end
;

--
==================================================================
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
http://codeelixir.com
==================================================================

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Childs 2007-04-27 04:02:19 Re: Selecting rows with "static" ordering
Previous Message chester c young 2007-04-27 00:35:43 Re: Selecting rows with "static" ordering