Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 34A8D1337BA2 for ; Tue, 1 Mar 2011 19:46:45 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 28549-05 for ; Tue, 1 Mar 2011 23:46:37 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qw0-f46.google.com (mail-qw0-f46.google.com [209.85.216.46]) by mail.postgresql.org (Postfix) with ESMTP id 7A1A41337B9E for ; Tue, 1 Mar 2011 19:46:37 -0400 (AST) Received: by qwd7 with SMTP id 7so4125440qwd.19 for ; Tue, 01 Mar 2011 15:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=cLWzci+OeGGEjEzwNX2q4j/QGBoEEn5As5velEVYrCY=; b=MrWM7SNyJrKxWHe09RX31dbrGQOaO4LZI4Vwq3xQ2zPOTwQSJM+8/aFPyp7wFilsDO JTUNoPdhSlskkSe6KhImQvKnrCY4IVuNsSMmNCBq5I5I7akVVVxNhakBzMhv+iRo104X ofMTASdO1fBJhH9wI4xI4TqR57QY0eDMwFw6k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; b=Q0Wis+ghA7VvVHSXeTn+QBejo0hFthXlsJKPf3QmKz9r4I9c/gy1//JFWZjnKZaPpz ew0hMdU7SAnMw8JvifcUmtVAM2co9YEM8cwnJ+G+cLBZOYUO6T4FaarnZLL9adLl82mW GG4tmxHpKY9rHJbsN/G2M2p2+bUhtSS9XirAs= Received: by 10.229.77.215 with SMTP id h23mr3219344qck.292.1299023196766; Tue, 01 Mar 2011 15:46:36 -0800 (PST) Received: from [192.168.1.100] (207-237-122-16.c3-0.wsd-ubr1.qens-wsd.ny.cable.rcn.com [207.237.122.16]) by mx.google.com with ESMTPS id t7sm4451150qcs.28.2011.03.01.15.46.35 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 Mar 2011 15:46:36 -0800 (PST) Message-ID: <4D6D84CE.9010200@gmail.com> Date: Tue, 01 Mar 2011 18:44:14 -0500 From: Lee Hachadoorian User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: distinguishing identical columns after joins References: <4D6D4EC2.70306@gmail.com> <4D6D7A97.7050704@gmail.com> In-Reply-To: <4D6D7A97.7050704@gmail.com> X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit 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/8 X-Sequence-Number: 35210 On 03/01/2011 06:00 PM, Rob Sargent wrote: > > On 03/01/2011 03:13 PM, S G wrote: >> On Tue, Mar 1, 2011 at 2:53 PM, Rob Sargent wrote: >>> >>> 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? >>> >>> -- >>> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) >>> To make changes to your subscription: >>> http://www.postgresql.org/mailpref/pgsql-sql >>> >> Rob, what you wrote certainly does work. But suppose you had to do >> that for a join with 50 columns in each table, and you really needed >> to see all those columns show up in the final result set, and >> furthermore, you needed to be able to identify each one uniquely in >> the final result set. Explicit renaming works, but it's tedious. >> Call me lazy. I'm hoping a column-renaming shortcut exists that works >> with the "SELECT *" concept. >> >> If such a shortcut doesn't exist, I believe it easily could exist >> utilizing the following syntax: >> >> SELECT >> (a).* AS a_, >> (b).* AS b_ >> FROM >> t1 AS a >> INNER JOIN >> t1 AS b >> ON >> a.col1 = b.col1 >> >> which currently discards the AS identifiers and defaults to the column >> names as identified in their respective tables. Though implementing >> this is another issue altogether... I'm just asking if such a shortcut >> already exists. >> >> Thanks! >> sg > I suspected this was the tack you were taking and would be mildly > surprised if it hasn't been requested before so I suspect some wise soul > will put us in the right direction. > > But I still wonder it isn't a receiver/UI issue. Does your reader know > the meaning of "a_" vs "b_" in a non-trivial self join? Wouldn't you > rather have the output as a_col1, b_col1, a_col2, b_col2 ... for easy > comparison. And who can make sense of a 100 column results set anyway?:) > I don't know if someone has come up with a workaround, but to begin with note that the docs specify that when using the * "it is not possible to specify new names with AS; the output column names will be the same as the table columns' names." Off the cuff, a possible workaround would be to create multiple views of your table that rename the columns, i.e. CREATE VIEW vw_a_t1 AS SELECT col1 AS a_col1, col2 AS a_col2, … FROM t1; CREATE VIEW vw_b_t1 AS SELECT col1 AS b_col1, col2 AS b_col2, … FROM t1; Then you would do your select as SELECT * FROM vw_a_t1 JOIN vw_b_t1 ON a_col1 = b_col1; If you were often self-joining the table 3 or more times, you would obviously have to create views vw_c_t1, vw_d_t1, etc. If you need to do this for several tables, you might be able to create a function to create the views. The function would take a table name and the desired prefix as parameters and programatically construct, then EXECUTE the CREATE VIEW statement. --Lee -- Lee Hachadoorian PhD Student, Geography Program in Earth & Environmental Sciences CUNY Graduate Center