Re: UNION and ORDER BY ... IS NULL ASC - Mailing list pgsql-sql

From Josh Berkus
Subject Re: UNION and ORDER BY ... IS NULL ASC
Date
Msg-id 200304041852.30121.josh@agliodbs.com
Whole thread Raw
In response to UNION and ORDER BY ... IS NULL ASC  ("A.M." <agentm@cmu.edu>)
Responses Re: UNION and ORDER BY ... IS NULL ASC  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
AM,

> CREATE TABLE test(a int);
> SELECT a FROM test UNION SELECT a FROM test ORDER BY a IS NULL ASC;
>
> returns:
>
> ERROR:  ORDER BY on a UNION/INTERSECT/EXCEPT result must be on one of
> the result columns

I do believe that this is per the SQL spec; UNIONs may only be sorted on the
output columns.

So if you did
SELECT a, (a IS NULL) as test_a
FROM test
UNION
SELECT a, (a IS NULL)
FROM test2
ORDER BY test_a

... it should work.

--
-Josh BerkusAglio Database SolutionsSan Francisco



pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: UNION and ORDER BY ... IS NULL ASC
Next
From: Tom Lane
Date:
Subject: Re: UNION and ORDER BY ... IS NULL ASC