Re: Bug: aliasing in ORDER BY when UNIONing

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marko Kreen <marko(at)l-t(dot)ee>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug: aliasing in ORDER BY when UNIONing
Date: 2001-02-19 05:26:44
Message-ID: 21062.982560404@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Marko Kreen <marko(at)l-t(dot)ee> writes:
> # select o.id from op o union all SELECT -1 order by o.id;
> ERROR: Relation 'o' does not exist
>>
>> This is not valid SQL. For one thing, the table alias "o" is not
>> visible outside the first component SELECT.
>>
>> Yes, I know 7.0 took it... but its handling of ORDER BY on UNION
>> was pretty darn broken.

> Doh. But if I have several tables with a field 'id'? Then only
> way is to use the column number?

You could assign column names:

SELECT o.id as id1, p.id as id2, ... UNION ... ORDER BY id1, id2;

> Anyway such stuff should be documented I guess. From current
> docs I read that it should work.

Where?

> I would have expected that one
> of the select's aliases would be transferred to ORDER BY but its
> not possible?

The first subselect's column names are transferred to ORDER BY.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hiroshi Inoue 2001-02-19 05:56:25 Re: floating point representation
Previous Message Chua King Hua 2001-02-19 04:40:02 postgreSQL on window98