Skip site navigation (1) Skip section navigation (2)

Re: order by and union

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Benoit Lion <Benoit(dot)Lion(at)ac-grenoble(dot)fr>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: order by and union
Date: 2001-07-11 17:53:39
Message-ID: Pine.LNX.4.30.0107111949440.679-100000@peter.localdomain (view raw or flat)
Thread:
Lists: pgsql-bugs
Benoit Lion writes:

> select nmn_can,pre_can,dat_nai,dci_sai,cnd.cod_spe,cnd.cod_pa1,decision
> , cnd.cod_pa2
> >from  cnd,cnd_grp, decision where cnd.cod_eta='0731043M'
> and cnd.num_can=cnd_grp.num_can and cnd_grp.typ_grp='D'
> and cnd_grp.dci_sai=decision.cod_dec
>
> union
>
> select nmn_can,pre_can,dat_nai,dci_sai,cnd.cod_spe,cnd.cod_pa1,decision
> , cnd.cod_pa2
> >from cnd,cnd_grp, decision where cnd.num_can=cnd_grp.num_can and
> cnd_grp.typ_grp='D'
> and cnd_grp.dci_sai=decision.cod_dec and cnd.cod_cat='520'
> and cnd.cod_eta in ( select cod_aff from affichage where
> cod_eta='0731043M' and cnd.cod_spe=affichage.cod_spe)
>
> order by cnd.cod_spe, cnd.cod_pa1,decision,nmn_can
>
> so i replace the orber by :
>
> order by 5,6,7,1

This is correct.  The table names from the union branches are not in
scope for the order by.  Consider this case

select cnd.cod_spe from cnd
union
select cnd.cod_spe from (select relname from pg_class) as cnd (cod_spe)
order by cnd.cod_spe;

Which "cnd" is meant here?  However, you should be able to write

order by cod_spe;

-- 
Peter Eisentraut   peter_e(at)gmx(dot)net   http://funkturm.homeip.net/~peter


In response to

pgsql-bugs by date

Next:From: Tom LaneDate: 2001-07-11 17:56:58
Subject: Re: ERROR: deferredTriggerGetPreviousEvent: event for tuple (0,9) not found
Previous:From: Kristis MakrisDate: 2001-07-11 17:51:46
Subject: Re: ERROR: deferredTriggerGetPreviousEvent: event for tuple(0,9) not found

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group