Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C92001337B96 for ; Tue, 1 Mar 2011 15:53:50 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 95847-01 for ; Tue, 1 Mar 2011 19:53:44 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out01.mta.xmission.com (out01.mta.xmission.com [166.70.13.231]) by mail.postgresql.org (Postfix) with ESMTP id 167D61337B34 for ; Tue, 1 Mar 2011 15:53:43 -0400 (AST) Received: from in02.mta.xmission.com ([166.70.13.52]) by out01.mta.xmission.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1PuVdd-0007OR-Nf; Tue, 01 Mar 2011 12:53:41 -0700 Received: from [208.187.158.94] (helo=[172.16.1.114]) by in02.mta.xmission.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1PuVdd-0002LM-2j; Tue, 01 Mar 2011 12:53:41 -0700 Message-ID: <4D6D4EC2.70306@gmail.com> Date: Tue, 01 Mar 2011 12:53:38 -0700 From: Rob Sargent User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Lightning/1.0b1 Thunderbird/3.0.11 MIME-Version: 1.0 To: pgsql-sql@postgresql.org References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-XM-SPF: eid=; ; ; mid=; ; ; hst=in02.mta.xmission.com; ; ; ip=208.187.158.94; ; ; frm=robjsargent@gmail.com; ; ; spf=neutral X-XM-AID: U2FsdGVkX1+wrjZpGdMTZpPNorSHafxlrxwNJMnO6H0= X-SA-Exim-Connect-IP: 208.187.158.94 X-SA-Exim-Mail-From: robjsargent@gmail.com X-Spam-DCC: XMission; sa07 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;pgsql-sql@postgresql.org X-Spam-Relay-Country: Subject: Re: distinguishing identical columns after joins X-SA-Exim-Version: 4.2.1 (built Fri, 06 Aug 2010 16:31:04 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.589 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201103/3 X-Sequence-Number: 35205 On 03/01/2011 12:47 PM, S G wrote: > This question is particularly geared towards self-joins, but can apply > to any join where the tables involved have any identical column names. > Aside from explicit column references, is there any way to pull all > columns (*) from each table in a join and quickly append/prepend some > identifier to distinguish them from each other? For example, table t1 > contains columns named col1 and col2: > > SELECT > * > FROM > t1 AS a > INNER JOIN > t1 AS b > ON > a.col1 = b.col1 > > would yield a result set with column names: col1, col2, col1, col2. > I'm looking for something that would automatically rename the columns > like: a_col1, a_col2, b_col1, b_col2. Does such functionality exist? > It's not such a big deal in this example, but it can be quite tedious > to explicitly reference and rename every single column for such joins > when the tables involved have a very large number of columns. > > I would beg for the same functionality when expanding compound > datatypes. For example, a compound datatype cd1 exists with fields > named f1 and f2: > > SELECT > ((value1, value2)::cd1).* AS a > > normally produces a result set with column names: f1, f2. I'm looking > for something that would produce column names: a_f1, a_f2. > > Thanks! > sg > select a.col1 as a_col1 etc doesn't do it for you?