gpt4 book ai didi

SQL 更新其连接值的 SUM

转载 作者:行者123 更新时间:2023-12-03 01:14:30 25 4
gpt4 key购买 nike

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

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 更新其连接值的 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2502032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com