Re: UNION with ORDER BY -allowed?

From: Chris Green <chris(at)areti(dot)co(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: UNION with ORDER BY -allowed?
Date: 2004-12-02 15:48:14
Message-ID: 20041202154814.GB29993@areti.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Dec 02, 2004 at 10:13:26AM -0500, terry(at)ashtonwoodshomes(dot)com wrote:
> > -----Original Message-----
> > From: pgsql-general-owner(at)postgresql(dot)org
> > [mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Chris Green
> > Sent: Thursday, December 02, 2004 9:56 AM
> > To: pgsql-general(at)postgresql(dot)org
> > Subject: [GENERAL] UNION with ORDER BY -allowed?
> >
> >
> > It's not quite clear (to me at least) whether I can have a UNION and
> > an ORDER BY in a SELECT statement.
> >
> > What I want to do is:-
> >
> > SELECT
> > col1, col2, col5, col6
> > FROM
> > table
> > WHERE
> > col2 = 'X'
> > UNION
> > SELECT
> > col3, col4, col5, col6
> > FROM
> > table
> > WHERE
> > col4 = 'X'
> > ORDER BY
> > coalesce(col1, col3)
> >
> > Is this valid syntax allowed by postgresql? (I'm not at the system
> > where postgresql is installed at the moment so I can't just try it)
> Yes, provided the columns are the same data types (or you can cast them to make them the same)
>
Yes, they are all matching types, what I said just below indicates
this (note that the other columns are the *same* columns).

> >
> > col1 and col3 are both DATE columns. col2 and col4 are both
> > varchar(1).
> >
> > I want the ORDER BY to order the result of the UNION.
>
> It does, per SQL spec. Nothing less would make sense if you ask me. :)
>
That's what I thought it was saying, thanks for confirming it.

--
Chris Green (chris(at)areti(dot)co(dot)uk)

"Never ascribe to malice, that which can be explained by incompetence."

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-12-02 16:26:45 Re: Poor Performance with Distinct Subqueries with EXISTS and EXCEPT
Previous Message Thierry Missimilly 2004-12-02 15:46:12 Re: pg_restore taking 4 hours!