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

From Rodrigo Rosenfeld Rosas
Subject Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Date
Msg-id 50511852.2020605@gmail.com
Whole thread Raw
In response to Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D  (Samuel Gendler <sgendler@ideasculptor.com>)
List 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.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: Samuel Gendler
Date:
Subject: Re: 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