Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D - Mailing list pgsql-sql

From Samuel Gendler
Subject Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Date
Msg-id CAEV0TzACi9xjLKDfZbvg=K3kdZmmPN2sqgc31NedkPou95ZvQg@mail.gmail.com
Whole thread Raw
In response to ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D  (Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>)
Responses Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
List pgsql-sql
you put a conditional clause in the order by statement, either by referencing a column that is populated conditionally, like this

select A, when B < C Then B else C end as condColumn, B, C, D
from ...
where ...
order by 1,2, 5

or

select A, when B < C Then B else C end as condColumn, B, C, D
from ...
where ...
order by A,condColumn, D

or you can just put the conditional statement in the order by clause (which surprised me, but I tested it)

select A, B, C, D
from ...
where ...
order by A,when B < C then B else C end, D



On Wed, Sep 12, 2012 at 2:44 PM, Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com> wrote:
This is my first message in this list :)

I need to be able to sort a query by column A, then B or C (which one
is smaller, both are of the same type and table but on different left
joins) and then by D.

How can I do that?

Thanks in advance,
Rodrigo.


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

pgsql-sql by date:

Previous
From: Rodrigo Rosenfeld Rosas
Date:
Subject: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Next
From: Rodrigo Rosenfeld Rosas
Date:
Subject: Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D