agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: agharta82@gmail.com <agharta82@gmail.com>
To: Voillequin, Jean-Marc <Jean-Marc.Voillequin@moodys.com>
To: pgsql-sql@lists.postgresql.org <pgsql-sql@lists.postgresql.org>
Subject: Re: Group by column alias where same-column-name already exists
Date: Wed, 12 Dec 2018 10:49:46 +0100
Message-ID: <d2f400b9-09e7-dc9b-0835-5c402dda1c38@gmail.com> (raw)
In-Reply-To: <1EC8157EB499BF459A516ADCF135ADCE3A00A7E5@LON-WGMSX712.ad.moodys.net>
References: <70aee131-8353-607e-0eff-6fb2b9928cf6@gmail.com>
<1EC8157EB499BF459A516ADCF135ADCE3A00A7E5@LON-WGMSX712.ad.moodys.net>
Nice solution!
Thank you so mutch!
Il 11/12/18 17:51, Voillequin, Jean-Marc ha scritto:
> "...group by 1, first_table.c2"
>
> should do the trick.
>
> -----Original Message-----
> From: agharta82@gmail.com [mailto:agharta82@gmail.com]
> Sent: Tuesday, December 11, 2018 5:40 PM
> To: pgsql-sql@lists.postgresql.org
> Subject: Group by column alias where same-column-name already exists
>
>
>
> CAUTION: This email originated from outside of Moody's. Do not click links or open attachments unless you recognize the sender and know the content is safe.
>
>
>
> Hi all,
>
> A little question about grouping by a computed column alias where other columns with same name exists.
>
> Take look at this query (don't take care about, it was created as a test to explain my question).
>
> select case when (second_table.c1 = 'X') then '1' else '2' end as c1,
> first_table.c2
> from (
> select 'A'::text as c1, 'B'::text as c2
> ) first_table
> inner join (
> select 'X'::text as c1
> union
> select 'W'::text as c1
> union
> select 'X'::text as c1
> ) second_table on (true)
> group by c1, first_table.c2
>
>
> I have a computed coulmn alias in select called c1 (select case when
> (second_table.c1 = 'X') then '1' else '2' end as c1)
>
> I want to group by that alias c1 (group by c1)
>
> BUT first_table has a c1 column and second_table has a c1 column too!
>
> If i run the query it returns "ERROR: column reference "c1" is ambiguous"
>
> Someone knows a way to solve this?
>
> Not replacing grou by alias with its case and without changing column names, oblivious.
>
> In other dbs (like firebird) main (computed ) select column alias name takes precedence in group by clause. So if i group by c1 that means the computed (case when.... ) c1 in that case.
>
>
> Best regards,
>
> Agharta
>
>
>
> -----------------------------------------
>
> Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.
>
> This email was sent to you by Moody’s Investors Service EMEA Limited
> Registered office address:
> One Canada Square
> Canary Wharf
> London, E14 5FA
> Registered in England and Wales No: 8922701
>
> -----------------------------------------
view thread (3+ messages)
Message-ID: <d2f400b9-09e7-dc9b-0835-5c402dda1c38@gmail.com>
Permalink: ../d2f400b9-09e7-dc9b-0835-5c402dda1c38@gmail.com/
Also on: postgresql.org/message-id/d2f400b9-09e7-dc9b-0835-5c402dda1c38@gmail.com
reply
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: agharta82@gmail.com, Jean-Marc.Voillequin@moodys.com, pgsql-sql@lists.postgresql.org
Subject: Re: Group by column alias where same-column-name already exists
In-Reply-To: <d2f400b9-09e7-dc9b-0835-5c402dda1c38@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox