Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX19S-0001rj-Uf for pgsql-sql@arkaria.postgresql.org; Wed, 12 Dec 2018 09:49:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gX19R-0001ly-9J for pgsql-sql@arkaria.postgresql.org; Wed, 12 Dec 2018 09:49:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX19Q-0001gk-TN for pgsql-sql@lists.postgresql.org; Wed, 12 Dec 2018 09:49:57 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX19J-0001ZJ-TW for pgsql-sql@lists.postgresql.org; Wed, 12 Dec 2018 09:49:55 +0000 Received: by mail-wm1-x332.google.com with SMTP id y139so5016102wmc.5 for ; Wed, 12 Dec 2018 01:49:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:disposition-notification-to :date:user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Fs6UUGO3BY/wl3mhx5a44qud+JEt9Wl5idht/CDqenU=; b=VGQWiL1Canj8KY1AbtG7ZZalpzua3u0SKyR7MxqpVX0APgNIYXHjyaw2GBPBo5d7xn 9vgXqFUy0EamKqESCMZnX5KGZ9MFXNGdfuPmOUlLj8ww9LNzoePNL+873zB+ZNSblUza 0LqGTDb9S8clKbFr3rThdWjlBsGqXIDJJ+yi2QTRpmtTxSdcktQ++TC8B/67vHS7gA+t QYiGBTiK/WmwCgm74JZ3mm6bM8d0uKIx8gcJzLsqj7++tPAIy1EoNTD/9Zk5IVVaMZke 3wEeNo4wtfVrFKqFbQHZq9bIzlEgxiOd1uXWZKBbSWgny/DVEH9Yh+4VB7JgZDANgq1z ayRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id :disposition-notification-to:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Fs6UUGO3BY/wl3mhx5a44qud+JEt9Wl5idht/CDqenU=; b=BcazE2VoCc0D77cJSTFowLy6ih+2ugjqdQRjRgrOQLMJbLG5NV9CSEYGENPoCZS9F3 41Q+bVIyvjA0q36MlxXhTha5ru3sZrXYte25tfQiCFfr80UD2ejEvNBpwOjC9zfSBRSy zKy72GIfbOBq+4nu3FlF5wSW8/rpiX/ZIVapIj7060QYQssMtupy92Rqu11JRVIWioLZ VlgOIsWZUDP0VDbU0Xs4YZfAI6qfmyrXLJrGh1t1yNjhP44e82thiJCu1xup8pqK6OUA u1ftX4+bB7INLhDTMp3WYapf7kg1AEkfoNO0qmw2/AjxxTsIGVtEHI/VcNC5/LRpILXz /NqQ== X-Gm-Message-State: AA+aEWbb469LXAXGcUfuoYwypWyJGuLkSN5ie+j2iMqcZyiUDyOfdnZR 48zVXi2Io153ktlG1V5yWMvOwls= X-Google-Smtp-Source: AFSGD/X8hBtDOgwCX0GNehsoLdw2I8cnzyQBOfmEv3dXPl1kWpbsfZGUYzH/GdXNQyffzl5jnfOXQw== X-Received: by 2002:a1c:c008:: with SMTP id q8mr5133722wmf.99.1544608187905; Wed, 12 Dec 2018 01:49:47 -0800 (PST) Received: from [192.168.1.80] (hf5.z1.infracom.it. [82.193.17.245]) by smtp.gmail.com with ESMTPSA id p4sm11648982wrs.74.2018.12.12.01.49.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Dec 2018 01:49:47 -0800 (PST) Subject: Re: Group by column alias where same-column-name already exists To: "Voillequin, Jean-Marc" , "pgsql-sql@lists.postgresql.org" References: <70aee131-8353-607e-0eff-6fb2b9928cf6@gmail.com> <1EC8157EB499BF459A516ADCF135ADCE3A00A7E5@LON-WGMSX712.ad.moodys.net> From: "agharta82@gmail.com" Message-ID: Disposition-Notification-To: "agharta82@gmail.com" Date: Wed, 12 Dec 2018 10:49:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 In-Reply-To: <1EC8157EB499BF459A516ADCF135ADCE3A00A7E5@LON-WGMSX712.ad.moodys.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: it-IT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 > > =20 > > CAUTION: This email originated from outside of Moody's. Do not click li= nks or open attachments unless you recognize the sender and know the cont= ent is safe. > > =20 > > 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 tes= t to explain my question). > > select case when (second_table.c1 =3D 'X') then '1' else '2' end as c1,= > first_table.c2 > from ( > =C2=A0=C2=A0=C2=A0 select 'A'::text as c1, 'B'::text as c2 > ) first_table > inner join ( > =C2=A0=C2=A0=C2=A0 select 'X'::text as c1 > =C2=A0=C2=A0=C2=A0 union > =C2=A0=C2=A0=C2=A0 select 'W'::text as c1 > =C2=A0=C2=A0=C2=A0 union > =C2=A0=C2=A0=C2=A0 select 'X'::text as c1 > )=C2=A0 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 =3D '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 ambiguou= s" > > Someone knows a way to solve this? > > Not replacing grou by alias with its case and without changing column n= ames, 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 c= omputed (case when.... ) c1 in that case. > > > Best regards, > > Agharta > > > > ----------------------------------------- > > Moody's monitors email communications through its networks for regulato= ry compliance purposes and to protect its customers, employees and busine= ss and where allowed to do so by applicable law. The information containe= d 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 thi= s message to the intended recipient, you are hereby notified that you hav= e received this message in error and that any review, dissemination, dist= ribution 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 delet= e the message and all of its attachments. Every effort is made to keep ou= r network free from viruses. You should, however, review this e-mail mess= age, as well as any attachment thereto, for viruses. We take no responsib= ility and have no liability for any computer virus which may be transferr= ed via this e-mail message. > > This email was sent to you by Moody=E2=80=99s Investors Service EMEA Li= mited > Registered office address: > One Canada Square > Canary Wharf > London, E14 5FA > Registered in England and Wales No: 8922701 > > -----------------------------------------