Re: Order By Question

From: Ian Harding <iharding(at)pakrat(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Order By Question
Date: 2001-01-23 01:20:39
Message-ID: 3A6CDC67.4A79076E@pakrat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Tristan Colson wrote:

> This seems like the answer must be pretty easy, but I can't think of it:
>
> In the following statement:
>
> select field1 from my_table where field2 in (3, 1, 2);
>
> How can I modify this statement so that the record are returned in the
> order of first those records having field2 = 3, then field2 = 1, then
> field2 = 2.
> As it stands, I am getting them returned in the order of the value of
> field1.

You can't =;^)

If you add field 2 to the select list and order by it, you can get them in
1,2,3 or 3,2,1 order.

Alternatively, you could add a case statement to your query to add a column
that would be used just to order the data. My syntax may be foobar but it
would be something like:

select field1,
CASE
WHEN field2 = 3 THEN 1
WHEN field2=1 THEN 2
WHEN field2=2 THEN 3
AS orderfield
from my_table
where field2 in (3,1,2)
order by orderfield;

- Ian

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2001-01-23 02:28:04 Re: Re: is there a mysql to postgresql sql converter?
Previous Message Tristan Colson 2001-01-23 01:01:24 Order By Question