Re: select random order by random - Mailing list pgsql-general

From Ron Mayer
Subject Re: select random order by random
Date
Msg-id 472B9B5F.9070101@cheapcomplexdevices.com
Whole thread Raw
In response to Re: =?UTF-8?Q?select_random_order_by_random?=  (Chris Browne <cbbrowne@acm.org>)
List pgsql-general
Chris Browne wrote:
> If I replicate your query, with extra columns, AND NAMES, I get the following:
>
> test@[local]:5433=# select random() as r1, random() as r2, random() as r3 from generate_series(1,10) order by
random();
>          r1         |         r2         |        r3
> --------------------+--------------------+-------------------
>  0.0288224648684263 |  0.904462072532624 |  0.27792159980163
>   0.144174488261342 |  0.406729203648865 | 0.452183415647596
>  ...
> It is indeed somewhat curious that the query parser chose to interpret
> that the "order by random()" was referring to column #1.

And even more curiously, IMHO, even specifying
column names isn't enough.  Note that this:

li=# select * from (select (random()*10)::int as a, (random()*10)::int as b from generate_series(1,10) order by a) as x
orderby b; 
 a | b
---+----
 0 |  8
 1 | 10
 3 |  4
 4 |  8
 5 |  1
 5 |  9
 6 |  4
 6 |  5
 8 |  4
 9 |  0
(10 rows)

is sorted by "a" even though the outermost "order by"
clause explicitly said to order by "b".

Seems like it's a known odd behavior ...
http://archives.postgresql.org/pgsql-general/2006-11/msg01523.php
http://archives.postgresql.org/pgsql-general/2006-11/msg01539.php
http://archives.postgresql.org/pgsql-general/2006-11/msg01544.php

pgsql-general by date:

Previous
From: "Jeffrey W. Baker"
Date:
Subject: Re: setting for maximum acceptable plan cost?
Next
From: Myshkin LeVine
Date:
Subject: Problem starting the server with Mac OSX