pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: A.M. <agentm@cmu.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: UNION and ORDER BY ... IS NULL ASC
Date: Fri, 4 Apr 2003 18:49:31 -0800 (PST)
Message-ID: <20030404184636.W530-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <1E4E6631-670A-11D7-89FA-0030657192DA@cmu.edu>


On Fri, 4 Apr 2003, A.M. wrote:

>   PostgreSQL 7.3.2 on i686-pc-linux-gnu, compiled by GCC 2.95.3
>
> I have come across some unexpected behavior while dealing with a UNION
> and ORDER BY. I'd like some advice. Here's a scenario where I want to
> order by null values:
>
> 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
>
> whereas:
>
> SELECT a FROM test UNION SELECT a FROM test ORDER BY a;
>
> works fine. The column name is the same in both queries, yet I get an
> error! Obviously, this is a gross oversimplification of what I want to
> do, but I couldn't get it working in this minimal case. I also tried
> using the column number, and that returns the same results as the name.
> What am I doing wrong? Thanks for any info.

Try something like:

select a from (select a from test union select a from test) as foo order
by a is null asc;

It's an unsupported extension (I don't think SQL would allow
"order by a is null" in the first place) so you'll have to workaround for
now.




view thread (4+ messages)  latest in thread

Message-ID: <20030404184636.W530-100000@megazone23.bigpanda.com>
Permalink:  ../20030404184636.W530-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/20030404184636.W530-100000@megazone23.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: sszabo@megazone23.bigpanda.com, agentm@cmu.edu
  Subject: Re: UNION and ORDER BY ... IS NULL ASC
  In-Reply-To: <20030404184636.W530-100000@megazone23.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox