Howto sort the result of UNION (without modifying its type)? - Mailing list pgsql-general

From Clemens Eisserer
Subject Howto sort the result of UNION (without modifying its type)?
Date
Msg-id BANLkTim55-tkTyyxGMLgQth-hqQsAyKsSg@mail.gmail.com
Whole thread Raw
Responses Re: Howto sort the result of UNION (without modifying its type)?
Re: Howto sort the result of UNION (without modifying its type)?
List pgsql-general
Hi,

I have a query where I UNION several select statements which return
IDs of type INTEGER.
This works well, if the IDs don't need to be sorted:
> SELECT id FROM table WHERE id IN ((select id FROM table WHERE ...) UNION (SELECT id  FROM table_ WHERE ....))


However I need the result the UNIONs to be sorted, so I tried the following:

> SELECT id FROM table WHERE id IN (SELECT col0 FROM ((select id FROM table WHERE ...) UNION (SELECT id  FROM table_
WHERE....)) AS col0 ORDER BY col0 OFFSET X LIMIT Y) 
FEHLER:  Operator existiert nicht: integer = record
ERROR: Operator does not exist: integer = record
LINE 1: Select id FROM table WHERE id IN (Select col0 FROM ...

Is there any way to get the results of UNIONs sorted, without
converting it to record?

Thanks, Clemens

PS: I know the query looks really stupid, however the queries I UNION
are auto-generated SQL.
I don't have much choice here to write the query more efficient :/

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: PostgreSQL backend process high memory usage issue
Next
From: Shianmiin
Date:
Subject: Re: PostgreSQL backend process high memory usage issue