UNION with ORDER BY -allowed?

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

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)

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.

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

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Sidney-Woollett 2004-12-02 15:09:30 Re: UNION with ORDER BY -allowed?
Previous Message Mark Pratt 2004-12-02 14:54:31 PostgreSQL Authors wanted