三列中的SQL SUM不起作用

时间:2016-02-18 12:22:12

标签: sql ms-access

我有一个结合两个查询的表达式,工作正常

SELECT FinalIncoming.ID, FinalIncoming.ProductCode, FinalIncoming.ProductName, 
  FinalIncoming.StandardCost, FinalIncoming.OnHand, FinalOutgoiing.Incoming, 
  FinalIncoming.Outgoing
FROM FinalIncoming INNER JOIN FinalOutgoiing ON FinalIncoming.ID = FinalOutgoiing.ID;

但每当我尝试使用以下内容对最后三列进行补充时:

Sum([FinalIncoming.OnHand]+[FinalIncoming.Outgoing]-[FinalOutgoiing.Incoming])

Access出现错误Your query does not include the specified expression 'ID' as part of an aggregate function.

有谁知道我在这里做错了什么?我尝试用不同的方式重写SUM表达式,并使用别名,但它会出现相同的错误。

仅供参考:我知道Outgoiing查询拼写错误。

1 个答案:

答案 0 :(得分:0)

错误消息中的关键字是“作为聚合的一部分”

当您按功能分组使用时,ACCESS SQL要求所有列都参与聚合。

所以你的查询将是:

SELECT FinalIncoming.ID, FinalIncoming.ProductCode, FinalIncoming.ProductName, 
      first(FinalIncoming.StandardCost) as firstCostValue, SUM(FinalIncoming.OnHand + FinalOutgoiing.Incoming - FinalIncoming.Outgoing) AS something
    FROM FinalIncoming INNER JOIN FinalOutgoiing ON FinalIncoming.ID = FinalOutgoiing.ID
GROUP BY FinalIncoming.ID, FinalIncoming.ProductCode, FinalIncoming.ProductName;