Re: [GENERAL] Howto sort the result of UNION (without modifying its type)?
Clemens Eisserer Friday 08 April 2011 23:44:21 > 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 :/ May you try construct like this: SELECT i FROM (/* Your query example: */ SELECT i from v1 union select i from v2) as alias order by i; ? -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] Howto sort the result of UNION (without modifying its type)?
Clemens Eisserer Friday 08 April 2011 23:44:21 > 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 :/ May you try construct like this: SELECT i FROM (/* Your query example: */ SELECT i from v1 union select i from v2) as alias order by i; ? P.S. Sorry if this message was sent twice. -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] Howto sort the result of UNION (without modifying its type)?
Hi Robert, > does this work for you? > select u.id from (your unions) as u order by u.id Unfourtunatly not, it converts my union-results from INTEGER to RECORD. However, it seems to be possible to order the unions directly: > result1 UNION result2 ORDER BY u.id Hmm, the query plan looks really insane for this ... wonder how it will perform when the tables will not be mostly empty ;) Thanks, Clemens -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] Howto sort the result of UNION (without modifying its type)?
On 04/08/2011 03:44 PM, Clemens Eisserer wrote: 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 :/ does this work for you? select u.id from (your unions) as u order by u.id -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
[GENERAL] Howto sort the result of UNION (without modifying its type)?
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 :/ -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general