pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mathieu Arnold <mat@mat.cc>
To: mario.splivalo@mobart.hr
To: pgsql-sql@postgresql.org
Subject: Re: ORDER BY CASE ...
Date: Mon, 13 Feb 2006 16:39:12 +0100
Message-ID: <9693350566E722C466358E08@[192.168.9.47]> (raw)
In-Reply-To: <1139844930.13054.23.camel@localhost.localdomain>
References: <1139844930.13054.23.camel@localhost.localdomain>



+-le 13/02/2006 16:35 +0100, Mario Splivalo a dit :
| 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+01
|  816579 | +385915912312 | 2005-11-23 17:51:06+01
|  816595 | +38598539263  | 2005-11-23 17:58:21+01
|  816594 | +385915929232 | 2005-11-23 17:57:30+01
|  816589 | +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+01
|  816579 | +385915912312 | 2005-11-23 17:51:06+01
|  816595 | +38598539263  | 2005-11-23 17:58:21+01
|  816594 | +385915929232 | 2005-11-23 17:57:30+01
|  816589 | +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. 

Well, no, in the first, the result of the CASE is 2, and in the second 3, it
means that for every line, it'll sort using "2" as value for the first, and
"3" for the second query.

-- 
Mathieu Arnold



view thread (7+ messages)  latest in thread

Message-ID: <9693350566E722C466358E08@[192.168.9.47]>
Permalink:  ../9693350566E722C466358E08@%5B192.168.9.47%5D/
Also on:    postgresql.org/message-id/9693350566E722C466358E08@[192.168.9.47]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: mat@mat.cc, mario.splivalo@mobart.hr
  Subject: Re: ORDER BY CASE ...
  In-Reply-To: <9693350566E722C466358E08@[192.168.9.47]>
  References: <1139844930.13054.23.camel@localhost.localdomain> <9693350566E722C466358E08@[192.168.9.47]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox