SQL 更新其连接值的 SUM
我正在尝试将数据库中的字段更新为其连接值的总和:
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."
有什么想法吗?
I'm trying to update a field in the database to the sum of its joined values:
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
When I run this I get the following error:
"An aggregate may not appear in the set list of an UPDATE statement."
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个怎么样:
How about this:
上述解决方案的替代方案是使用表别名:
An alternate to the above solutions is using Aliases for Tables:
我遇到了同样的问题,发现可以用 公用表表达式(在 SQL 2005 或更高版本中可用):
I ran into the same issue and found that I could solve it with a Common Table Expression (available in SQL 2005 or later):
这是一个有效的错误。请参阅此。以下(以及下面建议的其他方法)是实现这一目标的方法:-
This is a valid error. See this. Following (and others suggested below) are the ways to achieve this:-
你需要这样的东西:
You need something like this :
对于 postgres,我必须调整解决方案以使其适合我:
With postgres, I had to adjust the solution with this to work for me:
使用类似于下面的子查询。
Use a sub query similar to the below.