Aggregate function in an SQL update query?

前端 未结 6 1840
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 10:48

I\'m trying to set the value in one table to the sum of the values in another table. Something along these lines:

UPDATE table1
SET field1 = SUM(table2.fiel         


        
6条回答
  •  孤城傲影
    2020-12-02 11:24

    You can also use CTE like below.

    ;WITH t2 AS (
        SELECT field3, SUM(field2) AS field2
        FROM table2
        GROUP BY field3
    )
    UPDATE table1
    SET table1.field1 = t2.field2
    FROM table1
    INNER JOIN t2 ON table1.field3 = t2.field3
    

提交回复
热议问题