我正在使用SQL Server 2008 R2。我使用以下查詢查看包中所有產品的價格時間總和。通過從SQL Server 2008 R2中的另一個表中獲取總和來更新表條目
select
a.nid, a.packagename,
case
when sum(b.quantity * c.price) is null
then 0
else sum(b.quantity * c.price)
end
from
tblpackage as a
left join
tblpackage_service as b on a.nid = b.package_id
left join
tblproduct as c on c.nid = b.service_id
group by
a.nid, a.packagename
返回以下結果
nid packagename (No Column Name)
---------------------------------------
6 Test Package 4 400.00
3 Test Package 5 0.00
5 Test Package 6 1350.00
8 Test Package 7 350.00
7 Test Package 8 400.00
9 Test Package 9 400.00
1 Test Package 1 0.00
2 Test Package 2 0.00
4 Test Package 3 3900.00
現在我使用
update tblpackage
set tblpackage.amount = case
when sum(b.quantity * c.price) is null
then 0
else sum(b.quantity * c.price)
end
from tblpackage AS a
left join tblpackage_service as b on a.nid = b.package_id
left join tblproduct as c on c.nid = b.service_id
更新相同的,我得到一個錯誤:
An aggregate may not appear in the set list of an UPDATE statement.
那麼,有什麼辦法使用更新來完成此操作查詢。
請幫忙。提前致謝。
它的工作原理,謝謝 – rexroxm