Обсуждение: ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D

Поиск
Список
Период
Сортировка

ORDER BY COLUMN_A, (COLUMN_B or COLUMN_C), COLUMN_D

От
Rodrigo Rosenfeld Rosas
Дата:
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.



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

От
Samuel Gendler
Дата:
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.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

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

От
Rodrigo Rosenfeld Rosas
Дата:
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



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

От
Rodrigo Rosenfeld Rosas
Дата:
Em 12-09-2012 19:34, Gavin Flower escreveu: <blockquote cite="mid:50510E0C.8080704@archidevsys.co.nz" type="cite"><div
class="moz-cite-prefix">On13/09/12 09:44, Rodrigo Rosenfeld Rosas wrote:<br /></div><blockquote
cite="mid:CAGmv+wKCpc9VeyYp6nS9WL1vrtX=3+i87mFVMP4sEpB1LyPGQA@mail.gmail.com"type="cite"><pre wrap="">This is my first
messagein 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.


</pre></blockquote><font face="Courier New, Courier, monospace">...</font><font face="Courier New, Courier,
monospace"><br/> SELECT <br />     *<br /> FROM<br />     tabc t<br /> ORDER BY<br />     t.a,<br />     LEAST(t.b,
t.c),<br/>     t.d<br /> ...<br /></font></blockquote><br /> Thank you Gavin, I was looking for this LEAST function for
along time. I have tried MIN but it didn't work with strings.<br /><br /> I guess this will allow me to do what I want.
Thankyou so much!<br /><br /> Best,<br /> Rodrigo.<br /><br /> 

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

От
Gavin Flower
Дата:
<div class="moz-cite-prefix">On 13/09/12 09:44, Rodrigo Rosenfeld Rosas wrote:<br /></div><blockquote
cite="mid:CAGmv+wKCpc9VeyYp6nS9WL1vrtX=3+i87mFVMP4sEpB1LyPGQA@mail.gmail.com"type="cite"><pre wrap="">This is my first
messagein 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.


</pre></blockquote><font face="Courier New, Courier, monospace">I created a script '</font><font face="Courier New,
Courier,monospace"><font face="Courier New,       Courier, monospace">variable_sort_order.sql</font>'...<br /><br />
DROPTABLE IF EXISTS tabc;<br /><br /> CREATE TABLE tabc<br /> (<br />     id  serial PRIMARY KEY,<br />     a   int,<br
/>    b   int,<br />     c   int,<br />     d   int<br /> );<br /><br /><br /> INSERT INTO tabc (a, b, c, d)<br />
VALUES(generate_series(1, 6), <br />         3 * random(), <br />         3 * random(), <br />        
generate_series(1,5));       <br /><br /><br /> SELECT <br />     *<br /> FROM<br />     tabc t<br /> ORDER BY<br />
   t.a,<br />     LEAST(t.b, t.c),<br />     t.d<br /> /**/;/**/<br /><br /> gavin=> \i variable_sort_order.sql<br
/>DROP TABLE<br /> psql:variable_sort_order.sql:10: NOTICE:  CREATE TABLE will create implicit sequence "tabc_id_seq"
forserial column "tabc.id"<br /> psql:variable_sort_order.sql:10: NOTICE:  CREATE TABLE / PRIMARY KEY will create
implicitindex "tabc_pkey" for table "tabc"<br /> CREATE TABLE<br /> INSERT 0 30<br />  id | a | b | c | d <br />
----+---+---+---+---<br/>  25 | 1 | 0 | 3 | 5<br />   7 | 1 | 1 | 1 | 2<br />   1 | 1 | 3 | 2 | 1<br />  13 | 1 | 2 | 3
|3<br />  19 | 1 | 2 | 2 | 4<br />   8 | 2 | 0 | 2 | 3<br />  14 | 2 | 0 | 2 | 4<br />  26 | 2 | 2 | 1 | 1<br />  20 |
2| 1 | 2 | 5<br />   2 | 2 | 2 | 2 | 2<br />   3 | 3 | 0 | 2 | 3<br />  21 | 3 | 1 | 1 | 1<br />  27 | 3 | 1 | 3 | 2<br
/> 15 | 3 | 3 | 1 | 5<br />   9 | 3 | 3 | 2 | 4<br />   4 | 4 | 0 | 1 | 4<br />  10 | 4 | 3 | 0 | 5<br />  16 | 4 | 1 |
3| 1<br />  22 | 4 | 1 | 1 | 2<br />  28 | 4 | 2 | 3 | 3<br />  11 | 5 | 0 | 1 | 1<br />  17 | 5 | 0 | 3 | 2<br />  23
|5 | 1 | 1 | 3<br />   5 | 5 | 3 | 1 | 5<br />  29 | 5 | 3 | 2 | 4<br />  18 | 6 | 2 | 0 | 3<br />  12 | 6 | 1 | 1 |
2<br/>  24 | 6 | 3 | 1 | 4<br />  30 | 6 | 1 | 3 | 5<br />   6 | 6 | 3 | 2 | 1<br /> (30 rows)<br /><br /><br /><br
/><br/></font><br /> 

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

От
Craig Ringer
Дата:
On 09/13/2012 07:20 AM, Rodrigo Rosenfeld Rosas wrote:

> Thank you Gavin, I was looking for this LEAST function for a long time.
> I have tried MIN but it didn't work with strings.

In SQL, "MIN" is an aggregate function. It actually does work with 
strings, but only when used as an aggregate:

regress=# SELECT min(x.a) FROM ( VALUES ('blah'),('blah2'),('aaaa') ) x(a); min
------ aaaa
(1 row)

--
Craig Ringer