X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id B2B42475ECE for ; Thu, 17 Apr 2003 11:30:07 -0400 (EDT) Received: from email03.aon.at (WARSL401PIP6.highway.telekom.at [195.3.96.93]) by postgresql.org (Postfix) with SMTP id 7BAE4475D91 for ; Thu, 17 Apr 2003 11:30:06 -0400 (EDT) Received: (qmail 268316 invoked from network); 17 Apr 2003 15:30:06 -0000 Received: from m156p017.dipool.highway.telekom.at (HELO cantor) ([62.46.9.113]) (envelope-sender ) by qmail3rs.highway.telekom.at (qmail-ldap-1.03) with SMTP for ; 17 Apr 2003 15:30:06 -0000 From: Manfred Koizar To: Rajesh Kumar Mallah Cc: Christoph Haller , pgsql-sql@postgresql.org Subject: Re: getting rid of "Adding missing FROM-clause entry...." Date: Thu, 17 Apr 2003 17:30:06 +0200 Message-ID: References: <3E9EB59F.B01FBF60@rodos.fzk.de> <200304172021.28073.mallah@trade-india.com> In-Reply-To: <200304172021.28073.mallah@trade-india.com> X-Mailer: Forte Agent 1.8/32.548 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-32.2 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,REFERENCES, REPLY_WITH_QUOTES,USER_AGENT_FORTE autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/278 X-Sequence-Number: 12979 On Thu, 17 Apr 2003 20:21:28 +0530, Rajesh Kumar Mallah wrote: >actually my orignal problem is to update >10 *columns* in profile master first five comes >from first entry in another table This part is easy (using table and column names from your prototype): UPDATE t_a SET fname1=foo.fname , mname1=foo.mname FROM (SELECT DISTINCT ON (id) id, fname, mname FROM t_b ORDER BY id, con_id ) AS foo WHERE t_a.id = foo.id; > and remaining >5 columns comes from the second corresponding >row in the other table. This is a bit harder, because while DISTINCT ON (id) can be viewed as sort of LIMIT 1 OFFSET 0 for each group of equal ids, there is no such construct to select the *second* row of each group. So we build a subquery that does not contain the first row of each group and take the first row of the rest, i.e. the second row of the original group: UPDATE t_a SET fname2=foo.fname , mname2=foo.mname FROM (SELECT DISTINCT ON (b2.id) b2.id, b2.fname, b2.mname FROM t_b AS b1, t_b AS b2 WHERE b1.id = b2.id AND b1.con_id < b2.con_id ORDER BY b2.id, b2.con_id) AS foo WHERE t_a.id = foo.id; Note that this does not set xname2 to NULL where no second corresponding row exists. You might need a third UPDATE statement to do this. Servus Manfred