我有2个代码,我遇到了麻烦。我看了一些其他的答案但完全糊涂了。这是SQL代码..(它是Access,SQL直通查询)
UPDATE TOP (500) InventorySuppliers
SET BoydQuantityAvailable = SUM(Boyd0004Daily.QOH), Cost = Boyd0004Daily.Cost
FROM Boyd0004Daily INNER JOIN
InventorySuppliers ON Boyd0004Daily.LocalSKU = InventorySuppliers.LocalSKU
WHERE (InventorySuppliers.SupplierID = 4) AND (NOT (InventorySuppliers.BoydQuantityAvailable = SUM(Boyd0004Daily.QOH))) OR
(InventorySuppliers.SupplierID = 4) AND (NOT (InventorySuppliers.Cost = Boyd0004Daily.Cost))
它不喜欢SUM上的更新(Boyd0004Daily.QOH),但对于我的生活,我还没有弄清楚如何重写它,所以它做一个选择,然后用1个查询进行更新。
答案 0 :(得分:1)
您错过了group by
。每次使用聚合函数时,都必须包含一个包含所有未聚合的列的组。
您还在SUM
条款中使用WHERE
,但这不起作用。如果要根据汇总过滤结果,则必须使用HAVING
。
答案 1 :(得分:0)
你要做的第一件事是让选择出来:
select *
from InventorySuppliers i
inner join (select LocalSKU, SUM(QOH) QOH_SUM, MAX(Cost) Cost --max or min
from Boyd0004Daily
group by LocalSKU) a
on a.LocalSKU = i.LocalSKU
where i.SupplierID = 4
and ((i.BoydQuantityAvailable != a.QOH_SUM))
OR i.Cost != a.Cost))
然后将您的选择转换为更新:
update i
set i.BoydQuantityAvailable = a.QOH_SUM,
i.Cost = a.Cost
from InventorySuppliers i
inner join (select LocalSKU, SUM(QOH) QOH_SUM, MAX(Cost) Cost --max or min
from Boyd0004Daily
group by LocalSKU) a
on a.LocalSKU = i.LocalSKU
where i.SupplierID = 4
and ((i.BoydQuantityAvailable != a.QOH_SUM))
OR i.Cost != a.Cost))
我认为此查询应该适合您。我可能有错误,因为我无法测试您的数据。