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

From: Rodrigo Rosenfeld Rosas <rr(dot)rosas(at)gmail(dot)com>
To: Samuel Gendler <sgendler(at)ideasculptor(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 23:18:42
Message-ID: 50511852.2020605@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Replied just to Samuel and forgot to include the list in my reply. Doing
that now, sorry...

Em 12-09-2012 18:53, Samuel Gendler escreveu:
> 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

Thank you for your insight on this, Samuel, and for your quick answer :)

But I don't think it would solve the issue I have.

I'm developing a query builder for a search engine.

The user is able to query any amount of available filters. And some
fields may have any number of aggregate fields.

So, suppose you're looking for an event sponsored by some company.

In the events records there could be some fields like Sponsor, Sponsor
2, Sponsor 3 and Sponsor 4. Yes, I know it is not a good design choice,
but this is how the system I inherited works.

So, in the Search interface, there is no way to build OR statements. So,
there is a notion of aggregate fields where Sponsor is the aggregator
one and the others are aggregates from Sponsor. Only Sponsor shows up in
the Search UI.

So, suppose the user wants to sort by event location and then by sponsor.

If there are multiple sponsors for a given event I want to be able to
sort by the one that would be indexed first.

How could I create a generic query for dealing with something like this?

Thank you,
Rodrigo.

>
>
>
> On Wed, Sep 12, 2012 at 2:44 PM, Rodrigo Rosenfeld Rosas
> <rr(dot)rosas(at)gmail(dot)com <mailto: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
> <mailto:pgsql-sql(at)postgresql(dot)org>)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
>

In response to

Browse pgsql-sql by date

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