Re: order by using functions under unions - Mailing list pgsql-bugs

From Tom Lane
Subject Re: order by using functions under unions
Date
Msg-id 25313.989531450@sss.pgh.pa.us
Whole thread Raw
In response to order by using functions under unions  (pgsql-bugs@postgresql.org)
List pgsql-bugs
pgsql-bugs@postgresql.org writes:
> select name from temp where name like 'e%'
> union
> select name from temp where name not like 'e%'
> order by substr(name,1,4) asc;

This isn't supported.  7.1 knows that it can't do it:

regression=# select name from temp where name like 'e%'
regression-# union
regression-# select name from temp where name not like 'e%'
regression-# order by substr(name,1,4) asc;
ERROR:  ORDER BY on a UNION/INTERSECT/EXCEPT result must be on one of the result columns

It'd be nice to make it happen for the case you illustrate (ORDER BY on
an expression using only result columns) but that's not done yet.

In the meantime you can work around it (again, in 7.1) by using an
explicit subselect:

regression=# select name from (
regression(# select name from temp where name like 'e%'
regression(# union
regression(# select name from temp where name not like 'e%'
regression(# ) ss
regression=# order by substr(name,1,4) asc;
  name
---------
 chantal
 daniel
 eric
 ernst
(4 rows)

            regards, tom lane

pgsql-bugs by date:

Previous
From: pgsql-bugs@postgresql.org
Date:
Subject: order by using functions under unions
Next
From: pgsql-bugs@postgresql.org
Date:
Subject: Irix build failes (PSQL 7.1.1)