Re: Reordering results for a report - Mailing list pgsql-general

From Greg Stark
Subject Re: Reordering results for a report
Date
Msg-id 87u144w2b7.fsf@stark.dyndns.tv
Whole thread Raw
In response to Reordering results for a report  (Nathaniel Price <nprice@tesseract.net>)
List pgsql-general
Nathaniel Price <nprice@tesseract.net> writes:

> I'm new to this list, so I'm not sure if this is the right place to post this.
> If not, please direct me to where it would be better to post it.
>
> Anyway, I'm creating a report generation tool of sorts in PHP for a database.
> As part of this report generation tool, I'd like to allow the user to reorder
> these results arbitrarily. In other words:
>
> id | offer
> ---+------------
> 1  | Offer 1
> 2  | Offer 2
> 3  | Offer 3
>
> could become
>
> id | offer
> ---+------------
> 3  | Offer 3
> 1  | Offer 1
> 2  | Offer 2

You could do something like

select id,offer
  from tab
 order by case when id = 3 then 1 when id = 1 then 2 when id = 2 then 3 end

But a) if there are thousands of records you're going to have rather a huge
query and b) where are you going to store this ordering?

--
greg

pgsql-general by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Delete all records NOT referenced by Foreign Keys
Next
From: Greg Stark
Date:
Subject: Re: tablespaces in 7.5?