我正在尝试将数据库中的字段更新为其联接值的总和:

UPDATE P
SET extrasPrice = SUM(E.price)
FROM dbo.BookingPitchExtras AS E
INNER JOIN dbo.BookingPitches AS P ON E.pitchID = P.ID
    AND P.bookingID = 1
WHERE E.[required] = 1

运行此命令时,出现以下错误:
"An aggregate may not appear in the set list of an UPDATE statement."

有任何想法吗?

最佳答案

这个怎么样:

UPDATE p
SET p.extrasPrice = t.sumPrice
FROM BookingPitches AS p
INNER JOIN
    (
        SELECT PitchID, SUM(Price) sumPrice
        FROM BookingPitchExtras
        WHERE [required] = 1
        GROUP BY PitchID
    ) t
    ON t.PitchID = p.ID
WHERE p.bookingID = 1

关于sql - SQL更新其连接值的SUM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2502032/

10-09 00:26