SQL中的Sum()+ DCount()组合不能产生正确的结果

时间:2011-06-17 14:17:08

标签: ms-access ms-access-2007

如何获得Granny Smith苹果的总数?我已经尝试了一切,似乎没有任何工作。这是一个交叉表查询,我正在尝试计算每个运输容器中Granny_Smith苹果的数量。

MS Access 2007表达式

Total_Green_Apples: Sum(DCount("[Apple_Type]","[Apples]","[Apple_Type]"='Granny_Smith'))

SQL

TRANSFORM Count(DCount("[Apple_Type]","[Apples]")) AS Apple_Type
SELECT Shipping.Container_number, Sum(DCount("[Apple_Type]","[Apples]","[Apple_Type]"='Granny_Smith')) AS Total_Green_Apples
FROM Shipping INNER JOIN Apples ON Shipping.ID = Apples.ID
GROUP BY Shipping.Container_number
PIVOT Apples.Apple_Type;

请帮忙。

1 个答案:

答案 0 :(得分:1)

如果我正确地阅读了您的问题,您只需要计算所有运输容器中所有Granny Smith苹果的数量吗?

我有点猜测你的数据库结构,这就是我想出的:

SELECT COUNT(Apples.ID) as Total_Green_Apples
FROM Shipping
    JOIN  Apples ON Apples.ID = Shipping.ID
WHERE Apples.Apple_Type = 'Granny Smith'

如果你想在每个容器中计算所有Granny Smith苹果,那就是:

SELECT Shipping.ID, COUNT(Apples.ID) as Total_Green_Apples
FROM Shipping
    JOIN  Apples ON Apples.ID = Shipping.ID
WHERE Apples.Apple_Type = 'Granny Smith'
GROUP BY Shipping.ID

如果您提供表格定义,我可以调整查询。