Update a field with the sum value from another table

UPDATE a field from a different table

This snippet retrieves the value from table2.field2, and places it in table1.field1.

update table1 set table1.field1 = table2.field2
from table1,table2
where table1.key = table2.key

UPDATE a field with a SUM from a different table

This snippet adds up multiple entry’s in the second table which have the correct key, and places the sum in the first table. This technique is usefull for totaling seperate lines in table2 into a different table.

update table1 set field1 = ( select sum(table2.field2) from table2 where table1.key = table2.key)

Leave a Reply

*