X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9C1D9474E4F for ; Fri, 4 Apr 2003 21:49:31 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 0E86C474E42 for ; Fri, 4 Apr 2003 21:49:27 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 2A85CD62D; Fri, 4 Apr 2003 18:49:31 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 1FFC95C0B; Fri, 4 Apr 2003 18:49:31 -0800 (PST) Date: Fri, 4 Apr 2003 18:49:31 -0800 (PST) From: Stephan Szabo To: "A.M." Cc: Subject: Re: UNION and ORDER BY ... IS NULL ASC In-Reply-To: <1E4E6631-670A-11D7-89FA-0030657192DA@cmu.edu> Message-ID: <20030404184636.W530-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-25.8 required=5.0 tests=BAYES_00,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/76 X-Sequence-Number: 12777 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.