Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D

From: Samuel Gendler <sgendler(at)ideasculptor(dot)com>
To: Rodrigo Rosenfeld Rosas <rr(dot)rosas(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Date: 2012-09-12 21:53:48
Message-ID: CAEV0TzACi9xjLKDfZbvg=K3kdZmmPN2sqgc31NedkPou95ZvQg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)rosas(at)gmail(dot)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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gavin Flower 2012-09-12 22:34:52 Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Previous Message Rodrigo Rosenfeld Rosas 2012-09-12 21:44:15 ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D