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 1gWl4j-0002Bf-Kr for pgsql-sql@arkaria.postgresql.org; Tue, 11 Dec 2018 16:40:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gWl4h-0006h9-MI for pgsql-sql@arkaria.postgresql.org; Tue, 11 Dec 2018 16:39:59 +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 1gWl4h-0006h1-71 for pgsql-sql@lists.postgresql.org; Tue, 11 Dec 2018 16:39:59 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gWl4e-0004BN-JO for pgsql-sql@lists.postgresql.org; Tue, 11 Dec 2018 16:39:57 +0000 Received: by mail-wr1-x430.google.com with SMTP id u3so14812016wrs.3 for ; Tue, 11 Dec 2018 08:39:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:disposition-notification-to:date :user-agent:mime-version:content-transfer-encoding:content-language; bh=JQnfzM2eWDNEOZif3/GklHqBWuFy78JPCZxZuJCk1Fo=; b=n/7Vjwv6H1Y3ZUxd7FLuJ807NPChySkhujWcsbYviZV+tKIb8wxzOc0nPEYJvTi1gf cbS/EfdQEIsWQXDInYiM8FSLqgwPYuLYkp9z9dCCTvDQSjMJ67SOiGsYpdYXSV+lYoHf RSgFlOz2hpnDidgoYBdTOwjByXuGgHGz91ESVntkDviACu5MY7GJCzv0YRgu0EMX0AMR /Gp8HtL5EulWdfBxsearuAu/V81pRVIOBE+l6HZCy/l2OcowHlS/bO0L3j684k13y4IH GyJRQttPgVKHaXlikRqSHr6YwAbTuscMFiN4y4qMbjqgNsdkIxCFHr9M3viwRrVe4QOH gXMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id :disposition-notification-to:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=JQnfzM2eWDNEOZif3/GklHqBWuFy78JPCZxZuJCk1Fo=; b=giUpieYBcDnxu/dmltSAySgo0mBzNNh8pU0g+tD/E4Nas6JsPqy95MOLMMeAUlMQYI 3gs2eq/YxIJrSPv6dkuWOUjB3pnN9puq6zKZDD/jxu0t212mGEe+h8b+3tnJZnqzcV3A xrMptAuQBwh52fqdioAwh14s7o26HbmsX+z/ZAMHY6EJ18nmjgT2yW/iTZy158h96eEA H2k8GkPP//WoTLPrp51PUghQg2j3Js4XyFmTIUsTUAllt8KPEBqHfd4NqmnRks2ysxxM MkYLPZpRKV8N4lKiCW+qpa1ax3qIT6DggvQ9DWF2SGE+9vfrUvvwp7XScOx9KVWPW0Hp pX/g== X-Gm-Message-State: AA+aEWbzovNfCadIOnaLBTIrcyBpk/mvq+FErg2dld10ZkMEPM+/VHct 9hKLwjrrfzM9Bl92Lpaamg5N86Q= X-Google-Smtp-Source: AFSGD/UfNwvu4SkhMMRGHBKqn71Ak+raaREKdbCM+Pp8v5v+7eCEDWluTQLOcpvjQuOd3ZdPHzhZuA== X-Received: by 2002:adf:f149:: with SMTP id y9mr15321549wro.284.1544546393745; Tue, 11 Dec 2018 08:39:53 -0800 (PST) Received: from [192.168.1.80] (hf5.z1.infracom.it. [82.193.17.245]) by smtp.gmail.com with ESMTPSA id l202sm706510wma.33.2018.12.11.08.39.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Dec 2018 08:39:53 -0800 (PST) To: pgsql-sql@lists.postgresql.org From: "agharta82@gmail.com" Subject: Group by column alias where same-column-name already exists Message-ID: <70aee131-8353-607e-0eff-6fb2b9928cf6@gmail.com> Disposition-Notification-To: "agharta82@gmail.com" Date: Tue, 11 Dec 2018 17:39:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: it-IT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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