ORDER BY CASE ... - Mailing list pgsql-sql

From Mario Splivalo
Subject ORDER BY CASE ...
Date
Msg-id 1139844930.13054.23.camel@localhost.localdomain
Whole thread Raw
Responses Re: ORDER BY CASE ...
Re: ORDER BY CASE ...
List pgsql-sql
Am I misusing the ORDER BY with CASE, or, what? :)

I have a table, messages, half dozen of columns, exposing here just
three of them:

pulitzer2=# select id, "from", receiving_time from messages where
service_id = 20 order by case when 5=5 then 2 else 3 end desc limit 5;  id   |     from      |     receiving_time
--------+---------------+------------------------869585 | +385989095824 | 2005-12-08 16:04:23+01816579 | +385915912312
|2005-11-23 17:51:06+01816595 | +38598539263  | 2005-11-23 17:58:21+01816594 | +385915929232 | 2005-11-23
17:57:30+01816589| +385912538567 | 2005-11-23 17:54:32+01
 
(5 rows)


pulitzer2=# select id, "from", receiving_time from messages where
service_id = 20 order by case when 5=6 then 2 else 3 end desc limit 5;  id   |     from      |     receiving_time
--------+---------------+------------------------869585 | +385989095824 | 2005-12-08 16:04:23+01816579 | +385915912312
|2005-11-23 17:51:06+01816595 | +38598539263  | 2005-11-23 17:58:21+01816594 | +385915929232 | 2005-11-23
17:57:30+01816589| +385912538567 | 2005-11-23 17:54:32+01
 
(5 rows)


I tought I'd get differently sorted data, since in the first query I
said 5=5, and in second I said 5=6. 

Is this a right way to use CASE on ORDER BY, or? I need to sord the data
in the function depending on the function parametar. If it's true,
randomize the sort, if not, sort by receiving_time, newest first.

So, can I do it with ORDER BY CASE ... END, or do I need to have two
querries, and then first check for the value of the parametar, and then,
according to that value, call the SELECTs wich sort randomly, or by
receiving_time.
Mario

P.S. The postgres is 8.1.2. 
-- 
Mario Splivalo
Mob-Art
mario.splivalo@mobart.hr

"I can do it quick, I can do it cheap, I can do it well. Pick any two."




pgsql-sql by date:

Previous
From: "Foster, Stephen"
Date:
Subject: Re: Trigger/Sequence headache
Next
From: Mathieu Arnold
Date:
Subject: Re: ORDER BY CASE ...