<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 />