pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #14316: union all with order by "missing from-clause"
3+ messages / 2 participants
[nested] [flat]
* BUG #14316: union all with order by "missing from-clause"
@ 2016-09-08 09:45 ozzi_99@hotmail.com
0 siblings, 1 reply; 3+ messages in thread
From: ozzi_99@hotmail.com @ 2016-09-08 09:45 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 14316
Logged by: Ossi Kasurinen
Email address: ozzi_99@hotmail.com
PostgreSQL version: 9.5.4
Operating system: windows 8.1 pro
Description:
/*I cannot do "union all" operation with "order by" to another table than
the primary.
example tables and select clause:*/
--table creation: bar
CREATE TABLE public.bar
(
id integer NOT NULL,
sortcolumn integer,
CONSTRAINT pk_bar PRIMARY KEY (id)
)
--table creation:foo
CREATE TABLE public.foo
(
id integer NOT NULL,
barid integer,
sortcolumn integer,
CONSTRAINT pk_foo PRIMARY KEY (id),
CONSTRAINT fk_second FOREIGN KEY (barid)
REFERENCES public.bar (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
-- insert some dummy data.
-- the following doesn't work
select origin.id, origin.barid, origin.sortcolumn from foo origin
inner join bar on origin.barId = bar.Id
union all
select a.id, a.barid, a.sortcolumn from foo as a
inner join bar as b on a.barid = b.id
order by b.sortcolumn
limit 1
/* If I take "b" away from order by, it will order by table foo, and it
gives incorrect results. */
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #14316: union all with order by "missing from-clause"
@ 2016-09-08 11:19 Vitaly Burovoy <vitaly.burovoy@gmail.com>
parent: ozzi_99@hotmail.com
0 siblings, 1 reply; 3+ messages in thread
From: Vitaly Burovoy @ 2016-09-08 11:19 UTC (permalink / raw)
To: ozzi_99@hotmail.com; +Cc: pgsql-bugs
On 9/8/16, ozzi_99@hotmail.com <ozzi_99@hotmail.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 14316
> Logged by: Ossi Kasurinen
> Email address: ozzi_99@hotmail.com
> PostgreSQL version: 9.5.4
> Operating system: windows 8.1 pro
> Description:
>
> /*I cannot do "union all" operation with "order by" to another table than
> the primary.
>
> example tables and select clause:*/
> --table creation: bar
> CREATE TABLE public.bar
> (
> id integer NOT NULL,
> sortcolumn integer,
> CONSTRAINT pk_bar PRIMARY KEY (id)
> )
> --table creation:foo
> CREATE TABLE public.foo
> (
> id integer NOT NULL,
> barid integer,
> sortcolumn integer,
> CONSTRAINT pk_foo PRIMARY KEY (id),
> CONSTRAINT fk_second FOREIGN KEY (barid)
> REFERENCES public.bar (id) MATCH SIMPLE
> ON UPDATE CASCADE ON DELETE CASCADE
> )
>
> -- insert some dummy data.
> -- the following doesn't work
>
> select origin.id, origin.barid, origin.sortcolumn from foo origin
> inner join bar on origin.barId = bar.Id
> union all
> select a.id, a.barid, a.sortcolumn from foo as a
> inner join bar as b on a.barid = b.id
> order by b.sortcolumn
> limit 1
>
> /* If I take "b" away from order by, it will order by table foo, and it
> gives incorrect results. */
>
It is not a bug.
You have to wrap union by parenthesis if you want to order result of
union. It solves ambiguousness to which part order and limit should be
applied
(
select origin.id, origin.barid, origin.sortcolumn from foo origin
inner join bar on origin.barId = bar.Id
union all
select a.id, a.barid, a.sortcolumn from foo as a
inner join bar as b on a.barid = b.id
)
order by sortcolumn
limit 1
--
Best regards,
Vitaly Burovoy
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #14316: union all with order by "missing from-clause"
@ 2016-09-08 11:24 Vitaly Burovoy <vitaly.burovoy@gmail.com>
parent: Vitaly Burovoy <vitaly.burovoy@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: Vitaly Burovoy @ 2016-09-08 11:24 UTC (permalink / raw)
To: ozzi_99@hotmail.com; +Cc: pgsql-bugs
On 9/8/16, Vitaly Burovoy <vitaly.burovoy@gmail.com> wrote:
> On 9/8/16, ozzi_99@hotmail.com <ozzi_99@hotmail.com> wrote:
>> The following bug has been logged on the website:
>>
>> Bug reference: 14316
>> Logged by: Ossi Kasurinen
>> Email address: ozzi_99@hotmail.com
>> PostgreSQL version: 9.5.4
>> Operating system: windows 8.1 pro
>> Description:
>>
>> /*I cannot do "union all" operation with "order by" to another table than
>> the primary.
>>
>> example tables and select clause:*/
>> --table creation: bar
>> CREATE TABLE public.bar
>> (
>> id integer NOT NULL,
>> sortcolumn integer,
>> CONSTRAINT pk_bar PRIMARY KEY (id)
>> )
>> --table creation:foo
>> CREATE TABLE public.foo
>> (
>> id integer NOT NULL,
>> barid integer,
>> sortcolumn integer,
>> CONSTRAINT pk_foo PRIMARY KEY (id),
>> CONSTRAINT fk_second FOREIGN KEY (barid)
>> REFERENCES public.bar (id) MATCH SIMPLE
>> ON UPDATE CASCADE ON DELETE CASCADE
>> )
>>
>> -- insert some dummy data.
>> -- the following doesn't work
>>
>> select origin.id, origin.barid, origin.sortcolumn from foo origin
>> inner join bar on origin.barId = bar.Id
>> union all
>> select a.id, a.barid, a.sortcolumn from foo as a
>> inner join bar as b on a.barid = b.id
>> order by b.sortcolumn
>> limit 1
>>
>> /* If I take "b" away from order by, it will order by table foo, and it
>> gives incorrect results. */
>>
>
> It is not a bug.
>
> You have to wrap union by parenthesis if you want to order result of
> union. It solves ambiguousness to which part order and limit should be
> applied
>
> (
> select origin.id, origin.barid, origin.sortcolumn from foo origin
> inner join bar on origin.barId = bar.Id
> union all
> select a.id, a.barid, a.sortcolumn from foo as a
> inner join bar as b on a.barid = b.id
> )
> order by sortcolumn
> limit 1
P.S.: of course, if you want to select unsorted data from the first
table and ordered data from the second one, you just have to wrap the
second query:
select origin.id, origin.barid, origin.sortcolumn from foo origin
inner join bar on origin.barId = bar.Id
union all
(
select a.id, a.barid, a.sortcolumn from foo as a
inner join bar as b on a.barid = b.id
order by b.sortcolumn
limit 1
)
--
Best regards,
Vitaly Burovoy
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2016-09-08 11:24 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08 09:45 BUG #14316: union all with order by "missing from-clause" ozzi_99@hotmail.com
2016-09-08 11:19 ` Vitaly Burovoy <vitaly.burovoy@gmail.com>
2016-09-08 11:24 ` Vitaly Burovoy <vitaly.burovoy@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox