Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TIMLN-0006Po-TC for pgsql-sql@postgresql.org; Sun, 30 Sep 2012 16:26:14 +0000 Received: from smtp108.prem.mail.ac4.yahoo.com ([76.13.13.47]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TIMLK-0003pM-V3 for pgsql-sql@postgresql.org; Sun, 30 Sep 2012 16:26:13 +0000 Received: (qmail 53714 invoked from network); 30 Sep 2012 16:26:09 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=Ask/BjjkYCHKJQ3PeWAr5AxuN7fCdqJHSk8YoDQofBTxKQ+9umRUr6lZjsWb/TjS5uYhm/wWyVPgfWFhaImu3NmEUtGJNs87hu69R6jXsf5LAcdL51mbrvoD/1RCkuKK5ouUGPSXw52UJ+AoITVq3Yiw44F7pT7sBKwiKC+ZRPs= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1349022369; bh=glt493GSxfBh6tBi6lW7oGqnWeZ4Ietk/4BCXgTC2C4=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=PpZVl6fqxhCN9kZ1PA2asvpb1Dd3YEr3sBdcLrb05P1T95gsjmNrPkVjXu65dQKw4PQuj46ZCrN885Lg8IVOfXx2jHHELXUnCMaQc+jxZmvITvyF+beIP8rGu/xYaGD8VayWP3CAG3CS9DWyPFaNulONS909WRLvDzJmY8vlVZw= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: qf_J7pMVM1k6Y3YFjRG8E7kHePk8PH.FprYFXpDzcAL5I8s 4G4hFAt8U3.EZ_mKh8Q.SncIPusH48gDgj49cf_KaVB.9RY9Gvoyp3X0.6e_ cKo7v0Tq93Z.pVVtdb2qYGiu6THT9bYATYb4NsnVjbPkAF8M5EYNUHgUTwU7 U67wKBhrM0M.TldsaGzldDMtdSmFeSXStN8FBq3tL.g2adb._qmmbQXejBUO XxTbOTtrlJ3LCodGmIYHjs763oZ11ERHAmq74p9sXpkMQD_WsB1wKkQDghhd diInojKtbcTXvWgtDyG.j0kyJKaXylekKd1TyAZG36vsRSoOnxmBaMUGvm2Z 7HG0HECixf22TjIl6hlUAVkfS9Z.83CkASjdtQrgLETYuNntCdawxYiLQGc5 hhlikTttnokAtmH97Qd5nDBHdDF4p9PzNVQnGz.FjndYhuvlPr8Wf96Rt5QD m7biR X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp108.prem.mail.ac4.yahoo.com with SMTP; 30 Sep 2012 09:26:09 -0700 PDT From: "David Johnston" To: "'Matthias Nagel'" , References: <43516431.HfO3TYfNBy@hek506> <301621609.148693.1348931598720.JavaMail.open-xchange@ox.ims-firmen.de> <2031491.bojg3UQVmk@hek506> In-Reply-To: <2031491.bojg3UQVmk@hek506> Subject: Re: Reuse temporary calculation results in an SQL update query [SOLVDED] Date: Sun, 30 Sep 2012 12:26:00 -0400 Message-ID: <005e01cd9f28$481c39d0$d854ad70$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQHv9xOxWZP3/uYjb4rfEQresSPOSAHdk8KYAY3t8MEB0UZRepc0e1JA Content-Language: en-us X-Pg-Spam-Score: -4.1 (----) X-Archive-Number: 201209/69 X-Sequence-Number: 36871 >=20 > thank you. The "WITH" clause did the trick. I did not even know that = such a > thing exists. But as it turns out it makes the statement more readable = and > elegant but not faster. >=20 > The reason for the latter is that both the CTE and the UPDATE = statement > have the same "FROM ... WHERE ..." part, because the tempory = calculation > needs some input values from the same table. Hence the table is looked = up > twice instead once. This is unusual; the only WHERE clause you should require is some kind = of key matching... Like: UPDATE tbl SET .... FROM ( WITH final_result AS ( SELECT pkid, .... FROM tbl WHERE ... ) -- /WITH SELECT pkid, .... FROM final_result ) src -- /FROM WHERE src.pkid =3D tbl.pkid ; If you provide an actual query better help may be provided. David J.