使用聚合函数SUM更新

时间:2016-01-19 09:41:30

标签: sql ms-access ms-access-2010 ms-access-2007 aggregate-functions

我有以下SQL语句正常工作:

SELECT  SUM(IIF(srsFromStock, srsAmount, 0)), srvID
FROM tblServices AS S INNER JOIN tblServicesSub AS SS ON S.srvID =
SS.srssrvID 
GROUP BY srvID

我想将此选项转换为更新,但我还没有成功:

UPDATE tblServices AS S INNER JOIN tblServicesSub AS SS ON S.srvID =
SS.srssrvID  SET srvSumFromStock = SUM(IIF(srsFromStock, srsAmount,
0)) GROUP BY srvID

发生了什么,我该如何解决?

1 个答案:

答案 0 :(得分:1)

您无法使用汇总值进行更新。如果我在这种情况下,我将使用表变量或临时表来保存聚合结果,然后连接表并更新它们。像这样的事情:

xy <- c("Svarog-A", "Knez-B", "Petovia-C", "Svarun-D", "Hotimir-D")

splitxy <- strsplit(xy, "-")

xy.names <- sapply(splitxy, "[", 1)
xy.letters <- sapply(splitxy, "[", 2)

> xy.names
[1] "Svarog"  "Knez"    "Petovia" "Svarun"  "Hotimir"
> xy.letters
[1] "A" "B" "C" "D" "D"