X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 51E48D1B57E for ; Tue, 23 Sep 2003 16:16:48 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 15033-04 for ; Tue, 23 Sep 2003 13:16:16 -0300 (ADT) Received: from main.gmane.org (main.gmane.org [80.91.224.249]) by svr1.postgresql.org (Postfix) with ESMTP id 6DF71D1B911 for ; Tue, 23 Sep 2003 13:16:11 -0300 (ADT) Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1A1pp0-0004OG-00 for ; Tue, 23 Sep 2003 18:15:26 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org Received: from sea.gmane.org ([80.91.224.252]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1A1pp0-0004O8-00 for ; Tue, 23 Sep 2003 18:15:26 +0200 Received: from news by sea.gmane.org with local (Exim 3.35 #1 (Debian)) id 1A1ppS-0001vm-00 for ; Tue, 23 Sep 2003 18:15:54 +0200 From: Jeff Kowalczyk Subject: updating a field with a SUM from another table Date: Tue, 23 Sep 2003 12:15:53 -0400 Lines: 33 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@sea.gmane.org User-Agent: Pan/0.14.0 (I'm Being Nibbled to Death by Cats!) X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=1.0 tagged_above=0.0 required=5.0 tests=BAYES_20, FORGED_YAHOO_RCVD, RCVD_IN_OSIRUSOFT_COM, USER_AGENT X-Spam-Level: * X-Archive-Number: 200309/272 X-Sequence-Number: 15250 My SQL is apparently a bit rusty, can anyone advise how to refactor this updatecharges query to work? I need to update a total-charges field in my orders table with the sum of the line-item charges in another table. The tables are related by the orderid column. Thanks for any help you can provide with updatecharges. Query: updatecharges (query I'm having trouble with) -------------------- UPDATE orders RIGHT JOIN orderchargetotals ON orders.orderid = orderchargetotals.orderid SET orders.chargeasbilled = orderchargetotals.orderchargeasbilled; Query: orderchargetotals ------------------------ SELECT ordercharges.orderid, SUM(ordercharges.orderchargeasbilled) AS orderchargeasbilled FROM ordercharges GROUP BY orderid ORDER BY orderid; Table: orders ------------- orderid, chargeasbilled, field1, field2, ... Table: ordercharges ------------------- orderchargeid, orderid, chargecode, ordercharge BTW, I do know its counter to relational precepts to store a total one could calculate at query-time, but I have reasons to do so at the the moment.