Re: ORDER BY col is NULL in UNION causes error?

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Mike Benoit <ipso(at)snappymail(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ORDER BY col is NULL in UNION causes error?
Date: 2006-12-27 01:39:14
Message-ID: CB303AB0-13D6-48FE-AA75-EE6553B528D5@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-12-27 03:26:03 Re: ORDER BY col is NULL in UNION causes error?
Previous Message Sandip G 2006-12-27 01:26:21 Re: NEED URGENT HELP....