From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ken Tanzer <ken.tanzer@gmail.com>
To: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Two columns with same name in subselect--any way to SELECT without *?
Date: Sat, 6 Jan 2018 21:10:09 -0800
Message-ID: <cac087e4-90f8-9de0-cc8e-f8bc979bf9fc@aklaver.com> (raw)
In-Reply-To: <CAD3a31UikwBZwLwAT-cxNDEFOvhHx2RnVT1RCUTR08mA+bpMRw@mail.gmail.com>
References: <CAD3a31UikwBZwLwAT-cxNDEFOvhHx2RnVT1RCUTR08mA+bpMRw@mail.gmail.com>
On 01/06/2018 08:46 PM, Ken Tanzer wrote:
> Hi. You can have multiple columns with the same name, and use it as a
> subselect, like this silly example:
>
> SELECT 'a' AS my_col,'b' AS my_col,'foo' AS other;
> SELECT * FROM (SELECT 'a' AS my_col,'b' AS my_col,'foo' AS other) foo;
>
> But is there any way to select either of those columns without taking
> all the fields with *?
>
> SELECT my_col,other FROM (select 'a' AS my_col,'b' AS my_col,'foo' AS other) foo;
> ERROR: column reference "my_col" is ambiguous
>
> I suspect there isn't, but just wondering if there's some way I'm not
> aware of.
?:
SELECT bar.my_col, foo.my_col FROM (SELECT 'a' AS my_col) as bar ,
(select 'b' AS my_col,'foo' AS other) foo;
my_col | my_col
--------+--------
a | b
Though I would think this would just be pushing the point where you get
confused what my_col is really pointing to down the road.
>
> I found myself wanting to specify the columns by position, similar to
> ORDER or GROUP. (*.1, foo.1 or something like that), but that's probably
> wishful thinking!
>
> Cheers,
>
> Ken
>
>
>
> --
> AGENCY Software
> A Free Software data system
> By and for non-profits
> /http://agency-software.org//
> /https://demo.agency-software.org/client/
> ken.tanzer@agency-software.org <mailto:ken.tanzer@agency-software.org>
> (253) 245-3801
>
> Subscribe to the mailing list
> <mailto:agency-general-request@lists.sourceforge.net?body=subscribe> to
> learn more about AGENCY or
> follow the discussion.
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, ken.tanzer@gmail.com
Subject: Re: Two columns with same name in subselect--any way to SELECT without *?
In-Reply-To: <cac087e4-90f8-9de0-cc8e-f8bc979bf9fc@aklaver.com>
References: <CAD3a31UikwBZwLwAT-cxNDEFOvhHx2RnVT1RCUTR08mA+bpMRw@mail.gmail.com> <cac087e4-90f8-9de0-cc8e-f8bc979bf9fc@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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