在Access中显示每月分组的销售额?

时间:2013-06-03 17:14:31

标签: sql ms-access

我正在尝试显示每月分组的销售额,以显示类似:

201001 10000
201002 13000
201003 11000
201004 8000

为什么这不起作用?

SELECT [Transaction details].[Sales volume LOC]
FROM [Transaction details]
GROUP BY Month([Transaction details].[YYYY-MM-DD]);

我收到错误消息:“您的查询不包含指定表达式'Sales volume LOC'作为聚合函数的一部分。”

1 个答案:

答案 0 :(得分:2)

错误是因为您在聚合函数或GROUP BY子句中没有列[Sales volume LOC]。如果您想要每个月的总计,那么您应该将[Sales volume LOC]添加到sum()聚合函数中:

SELECT year([Transaction details].[YYYY-MM-DD]) as [Year],
  Month([Transaction details].[YYYY-MM-DD]) as [Month],
  sum([Transaction details].[Sales volume LOC]) as Total
FROM [Transaction details]
GROUP BY year([Transaction details].[YYYY-MM-DD]), Month([Transaction details].[YYYY-MM-DD])
ORDER BY year([Transaction details].[YYYY-MM-DD]), Month([Transaction details].[YYYY-MM-DD]);