Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-sql by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group