Thread: Intersect and order by

Intersect and order by

From
Michaël Fiey
Date:
Hi,

I don't understand what's happening with my command
I read postgresql docs, it should work...What's the problem ?
The command is :

select col1
from table
where <conditions 1>
intersect
select col1
from table
where <conditions 2>
order by col1

It does not work and return "get_sortgroupclause_expr: ORDER/GROUP BY
expression not found in targetlist"

I use pgsql 7.03 ont redhat 7.0.

Any help would be appreciated

michael




Re: Intersect and order by

From
Michael Fork
Date:
Either of these should fix your problem:

SELECT col1 as my_col
FROM table
WHERE <conditions 1>
INTERSECT
SELECT col1 as my_col
FROM table
WHERE <conditions 2>
ORDER BY my_col

- OR -

SELECT col1
FROM table
WHERE <conditions 1>
INTERSECT
SELECT col1
FROM table
WHERE <conditions 2>
ORDER BY 1
        ^^^
        Ordinal value of field to order by

To use UNION/INTERSECT/EXCEPT with ORDER BY, you must order by the ordinal
value of the field or by the aliased field name.

Michael Fork - CCNA - MCP - A+
Network Support - Toledo Internet Access - Toledo Ohio

On Wed, 28 Feb 2001, [iso-8859-1] Micha�l Fiey wrote:

> Hi,
>
> I don't understand what's happening with my command
> I read postgresql docs, it should work...What's the problem ?
> The command is :
>
> select col1
> from table
> where <conditions 1>
> intersect
> select col1
> from table
> where <conditions 2>
> order by col1
>
> It does not work and return "get_sortgroupclause_expr: ORDER/GROUP BY
> expression not found in targetlist"
>
> I use pgsql 7.03 ont redhat 7.0.
>
> Any help would be appreciated
>
> michael
>
>
>


Re: Intersect and order by

From
Tom Lane
Date:
=?iso-8859-1?Q?Micha=EBl_Fiey?= <m.fiey@futuresoundtech.com> writes:
> It does not work and return "get_sortgroupclause_expr: ORDER/GROUP BY
> expression not found in targetlist"

Just one of the many bugs in our old UNION/INTERSECT/EXCEPT code :-(

It's fixed for 7.1.

            regards, tom lane