UPDATE语句中使用聚合函数SUM
use mims_20211101
update memberinfo_2021 set Balance=e.countValue
from (
select d.MemberKey,isNull(c.countValue,-1)as countValue from(
select sum(ReturnType*ReturnValue) as countValue,MemberKey from ReturnInfo where Convert(nvarchar(10),ReturnDate,112)<'20210101' Group By MemberKey) c right join memberinfo_2021 as d on d.MemberKey=c.MemberKey)
e,memberinfo_2021
where memberinfo_2021.MemberKey=e.MemberKey