Re: Selecting rows with "static" ordering - Mailing list pgsql-sql

From Peter Childs
Subject Re: Selecting rows with "static" ordering
Date
Msg-id a2de01dd0704262102o3fd14c7r7700a0f20f209e27@mail.gmail.com
Whole thread Raw
In response to Re: Selecting rows with "static" ordering  ("Aaron Bono" <postgresql@aranya.com>)
List pgsql-sql
On 27/04/07, Aaron Bono <postgresql@aranya.com> wrote:
> On 4/26/07, Steve Midgley <public@misuse.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
> ;
>

could you not use the values statment of psql 8.2 hmmm

select * from table, values ((1,3),(2,2),(3,5),(4,1),(5,4)) as values
where values.id=table.second order by values.first;

never done it but I think it should work with a bit of playing arond...

Peter.


pgsql-sql by date:

Previous
From: "Aaron Bono"
Date:
Subject: Re: Selecting rows with "static" ordering
Next
From: Dmitry Turin
Date:
Subject: Re: Fwd: Re[2]: We all are looped on Internet: request + transport = invariant