X-Original-To: pgsql-docs-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 742F09DC81A; Thu, 12 Jan 2006 18:17:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34580-09; Thu, 12 Jan 2006 18:17:30 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from fetter.org (dsl092-188-065.sfo1.dsl.speakeasy.net [66.92.188.65]) by postgresql.org (Postfix) with ESMTP id 4EA8E9DCE1A; Thu, 12 Jan 2006 18:17:25 -0400 (AST) Received: by fetter.org (Postfix, from userid 500) id 9ECA5CED83; Thu, 12 Jan 2006 14:17:27 -0800 (PST) Date: Thu, 12 Jan 2006 14:17:27 -0800 From: David Fetter To: PostgreSQL Docs , PostgreSQL Patches Subject: Example for UPDATE FROM with correllation Message-ID: <20060112221727.GA20039@fetter.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="a8Wt8u1KmwUX3Y2C" Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.109 required=5 tests=[AWL=0.109] X-Spam-Score: 0.109 X-Spam-Level: X-Archive-Number: 200601/21 X-Sequence-Number: 3411 --a8Wt8u1KmwUX3Y2C Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Folks, Please find enclosed a doc patch that adds an example of a correllated UPDATE. Cheers, D -- David Fetter david@fetter.org http://fetter.org/ phone: +1 415 235 3778 Remember to vote! --a8Wt8u1KmwUX3Y2C Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="correllated_update.diff" Index: doc/src/sgml/ref/update.sgml =================================================================== RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v retrieving revision 1.33 diff -c -r1.33 update.sgml *** doc/src/sgml/ref/update.sgml 12 Oct 2005 23:19:22 -0000 1.33 --- doc/src/sgml/ref/update.sgml 12 Jan 2006 22:04:40 -0000 *************** *** 213,218 **** --- 213,230 ---- (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation'); + Now that all the papers are signed, update the most recently closed + deal of the travelling salesperson who closed the Rocket Powered + Skates deal with the Acme Corporation. + + UPDATE employees SET last_closed_deal = deal.id + FROM accounts JOIN deals ON (account.id = deal.account_id) + WHERE deal.employee_id = employees.id + AND deal.name = 'Rocket Powered Skates' + AND accounts.name = 'Acme Corporation' + ORDER BY deal.signed_date DESC LIMIT 1; + + Attempt to insert a new stock item along with the quantity of stock. If the item already exists, instead update the stock count of the existing item. To do this without failing the entire transaction, use savepoints. --a8Wt8u1KmwUX3Y2C--