X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 840F14758F1 for ; Thu, 17 Apr 2003 12:52:20 -0400 (EDT) Received: from email03.aon.at (WARSL401PIP6.highway.telekom.at [195.3.96.93]) by postgresql.org (Postfix) with SMTP id 6A446475458 for ; Thu, 17 Apr 2003 12:52:19 -0400 (EDT) Received: (qmail 219930 invoked from network); 17 Apr 2003 16:52:21 -0000 Received: from m160p004.dipool.highway.telekom.at (HELO cantor) ([62.46.9.228]) (envelope-sender ) by qmail3rs.highway.telekom.at (qmail-ldap-1.03) with SMTP for ; 17 Apr 2003 16:52:21 -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 18:52:21 +0200 Message-ID: References: <3E9EB59F.B01FBF60@rodos.fzk.de> <200304172021.28073.mallah@trade-india.com> <200304172122.17761.mallah@trade-india.com> In-Reply-To: <200304172122.17761.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=-34.6 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, 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/283 X-Sequence-Number: 12984 On Thu, 17 Apr 2003 21:22:17 +0530, Rajesh Kumar Mallah wrote: >i think two of your queries will work for me. I hope so, but ... >I wrote: >> 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; ... this can be slow, if you have large groups of equal id, because each group blows up to n^2/2 rows. You might be better off with a subselect like SELECT DISTINCT ON (b2.id) b2.id, b2.fname, b2.mname FROM t_b AS b2 WHERE EXISTS (SELECT * FROM t_b AS b1 WHERE b1.id = b2.id AND b1.con_id < b2.con_id) ORDER BY b2.id, b2.con_id or SELECT b2.id, b2.fname, b2.mname FROM t_b AS b2 WHERE (SELECT count(*) FROM t_b AS b1 WHERE b1.id = b2.id AND b1.con_id < b2.con_id) = 1 The latter having the advantage of being standard SQL. You have to experiment a little to find out what works best for you. BTW, my proposed UPDATE statements don't handle this case very well: id | con_id | fname | mname ----+--------+-------+------- 1 | 1 | first | ... 1 | 1 | 2nd | ... 1 | 2 | 3rd | ... 1 | 2 | 4th | ... Servus Manfred