聚合不应出现在 UPDATE 语句的集合列表中 怎么解决?

niuzhouhai 2011-11-20 11:13:11
update purchase_box set total_value= sum(B.quantity*B.unit_price) from purchase_box A,purchase_sub_and_box B where A.id=B.box_id

执行结果如下:
聚合不应出现在 UPDATE 语句的集合列表中

请问怎么解决?
...全文
1219 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
niuzhouhai 2011-11-21
  • 打赏
  • 举报
回复
update A set total_value= (select sum(quantity*unit_price*B.form_money_rate) from purchase_sub_and_box  ) from purchase_box A,purchase_form B,
purchase_sub_and_box C,purchase_sub_form D where D.purchase_form_id=B.id
and D.purchase_sub_no=C.order_no and C.box_id=A.id
niuzhouhai 2011-11-21
  • 打赏
  • 举报
回复
问题又来了,还是同一个问题,我多乘以了一个汇率,就出错了,如下
update A set total_value= (select sum(quantity*unit_price*B.form_money_rate) from purchase_sub_and_box ) from purchase_box A,purchase_form B,
purchase_sub_and_box C,purchase_sub_form D where D.purchase_form_id=B.id
and D.purchase_sub_no=C.order_no and C.box_id=A.id

执行出错信息如下:
在包含外部引用的被聚合表达式中指定了多个列。如果被聚合的表达式包含外部引用,那么该外部引用就必须是该表达式中所引用的唯一的一列。

问题就在于B.form_money_rate这个汇率,如果去掉就能正常执行,但是这个必须的啊,请问这个问题如何处理啊?

[Quote=引用 2 楼 ssp2009 的回复:]
SQL code

update A set total_value= (select sum(quantity*unit_price) from
purchase_sub_and_box where box_id=A.id)
from purchase_box A
[/Quote]
niuzhouhai 2011-11-20
  • 打赏
  • 举报
回复
搞定了,用了你给的代码,谢谢啊,

[Quote=引用 2 楼 ssp2009 的回复:]
SQL code

update A set total_value= (select sum(quantity*unit_price) from
purchase_sub_and_box where box_id=A.id)
from purchase_box A
[/Quote]
快溜 2011-11-20
  • 打赏
  • 举报
回复
update A set total_value= (select sum(quantity*unit_price) from 
purchase_sub_and_box where box_id=A.id)
from purchase_box A
快溜 2011-11-20
  • 打赏
  • 举报
回复
update A set total_value= (select sum(B.quantity*B.unit_price) from 
purchase_sub_and_box where box_id=A.id)
from purchase_box A

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧