Re: ORDER BY col is NULL in UNION causes error? - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: ORDER BY col is NULL in UNION causes error?
Date
Msg-id CB303AB0-13D6-48FE-AA75-EE6553B528D5@seespotcode.net
Whole thread Raw
In response to ORDER BY col is NULL in UNION causes error?  (Mike Benoit <ipso@snappymail.ca>)
Responses Re: ORDER BY col is NULL in UNION causes error?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Dec 26, 2006, at 18:39 , Mike Benoit wrote:

> Fails
> ---------------------
> select * from income_tax_rate_us UNION select * from
> income_tax_rate_us
> order by state is null;
> ERROR:  ORDER BY on a UNION/INTERSECT/EXCEPT result must be on one of
> the result columns

Even though state is a column in both tables, the order by is using
an expression, rather than a column.

Should work:

SELECT *, state IS NULL AS state_is_null
FROM income_tax_rate_us
UNION
SELECT *, state IS NULL AS state_is_null
FROM income_tax_rate_us
ORDER BY state_is_null

This should also work:

SELECT *
FROM (
    SELECT *
    FROM income_tax_rate_us
    UNION
    SELECT *
    FROM income_tax_rate_us
    ) union_result
ORDER BY state IS NULL

I'm not sure of the underlying reasons why your query doesn't work,
but give these a shot.

Michael Glaesemann
grzm seespotcode net



pgsql-general by date:

Previous
From: Mike Benoit
Date:
Subject: ORDER BY col is NULL in UNION causes error?
Next
From: Tom Lane
Date:
Subject: Re: ORDER BY col is NULL in UNION causes error?