pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedUNION ALL: Apparently based on column order rather than on column name or alias
2+ messages / 2 participants
[nested] [flat]
* UNION ALL: Apparently based on column order rather than on column name or alias
@ 2018-12-06 14:48 Andreas Schmid <user462411@gmail.com>
2018-12-06 15:06 ` Re: UNION ALL: Apparently based on column order rather than on column name or alias Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Andreas Schmid @ 2018-12-06 14:48 UTC (permalink / raw)
To: pgsql-general
Hi list
I realized the following behaviour of UNION ALL:
SELECT 'a' AS col1, 'b' AS col2
UNION ALL
SELECT 'c' AS col1, 'd' AS col2;
returns:
col1 | col2
------+------
a | b
c | d
Now I switch the column aliases in the second SELECT-Statement:
SELECT 'a' AS col1, 'b' AS col2
UNION ALL
SELECT 'c' AS col2, 'd' AS col1;
This returns the same result:
col1 | col2
------+------
a | b
c | d
Same behaviour when working just with column names, no aliases.
So my conclusion is that the result of UNION ALL depends on the column
order, not on the column names or aliases. Is this the intended
behaviour? And is it documented somewhere? What I found is the last
sentence on https://www.postgresql.org/docs/current/queries-union.html
which says
"[...] they return the same number of columns and the corresponding
columns have compatible data types [...]"
It says nothing about column order, column names or aliases. Does this
obviously imply it's the column order?
Thank you for some clarification.
Andy
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: UNION ALL: Apparently based on column order rather than on column name or alias
2018-12-06 14:48 UNION ALL: Apparently based on column order rather than on column name or alias Andreas Schmid <user462411@gmail.com>
@ 2018-12-06 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2018-12-06 15:06 UTC (permalink / raw)
To: Andreas Schmid <user462411@gmail.com>; +Cc: pgsql-general
Andreas Schmid <user462411@gmail.com> writes:
> So my conclusion is that the result of UNION ALL depends on the column
> order, not on the column names or aliases. Is this the intended
> behaviour?
Yes, this is required by SQL spec. Matching by column name would
be used if you wrote a CORRESPONDING clause, but we don't implement
that feature.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2018-12-06 15:06 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-06 14:48 UNION ALL: Apparently based on column order rather than on column name or alias Andreas Schmid <user462411@gmail.com>
2018-12-06 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
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