pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
To: Samuel Gendler <sgendler@ideasculptor.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
Date: Wed, 12 Sep 2012 20:18:42 -0300
Message-ID: <50511852.2020605@gmail.com> (raw)
In-Reply-To: <CAEV0TzACi9xjLKDfZbvg=K3kdZmmPN2sqgc31NedkPou95ZvQg@mail.gmail.com>
References: <CAGmv+wKCpc9VeyYp6nS9WL1vrtX=3+i87mFVMP4sEpB1LyPGQA@mail.gmail.com>
	<CAEV0TzACi9xjLKDfZbvg=K3kdZmmPN2sqgc31NedkPou95ZvQg@mail.gmail.com>

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 <mailto: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
>     <mailto:pgsql-sql@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/pgsql-sql
>
>

view thread (6+ messages)  latest in thread

Message-ID: <50511852.2020605@gmail.com>
Permalink:  ../50511852.2020605@gmail.com/
Also on:    postgresql.org/message-id/50511852.2020605@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rr.rosas@gmail.com, sgendler@ideasculptor.com
  Subject: Re: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D
  In-Reply-To: <50511852.2020605@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox