Received: from argh.demon.co.uk (IDENT:grim@argh.demon.co.uk [193.237.6.55]) by hub.org (8.9.3/8.9.3) with ESMTP id WAA83566 for ; Sat, 1 May 1999 22:38:38 -0400 (EDT) (envelope-from grim@argh.demon.co.uk) Received: (from grim@localhost) by argh.demon.co.uk (8.8.7/8.8.7) id DAA14465 for pgsql-sql@postgresql.org; Sun, 2 May 1999 03:25:40 +0100 From: M Simms Message-Id: <199905020225.DAA14465@argh.demon.co.uk> Subject: Update problem I cannot work out To: pgsql-sql@postgresql.org Date: Sun, 2 May 1999 03:25:40 +0100 (BST) Content-Type: text I cannot work out how to do the following query testdb=> select * from t1; var1|var2 ----+---- 1| 2 2| 2 2| 3 (3 rows) testdb=> select * from t2; var1|var2 ----+---- 5| 2 9| 3 4| 4 (3 rows) I need to add to the value of each var1 in t2 the sum of all var1's in t1 that have the same value corresponding var2's testdb=> update t2 set var1=t2.var1+sum(t1.var1) where t1.var2=t2.var2; ERROR: parser: illegal use of aggregates or non-group column in target list I imagine that this is failing because it isnt sure which values to sum(), but I cannot for the life of me figure out how to solve this. My second idea was to do testdb=> update t2 set var1=t2.var1+t1.var1 where t2.var2=t1.var1; NOTICE: Non-functional update, only first update is performed UPDATE 1 This will only, obviously, add one of the values I need to add. The values I need to end up with, just so you know you are solving the right problem, are: testdb=> select * from t1; var1|var2 ----+---- 1| 2 2| 2 2| 3 (3 rows) testdb=> select * from t2; var1|var2 ----+---- 8| 2 11| 3 4| 4 (3 rows) Any help on this would be appreciated. I am a bit stuck here, and I am sure I am just missing something blindingly obvious. I am loathe to use a cursor to do this, because my actual dataset is upwards of 10,000 records, and I do not wish to do 10,000 different queries.