ms访问错误:您尝试执行的查询不包括' [TOTAL] / [FREQ]'作为聚合函数的一部分

时间:2016-08-17 04:44:15

标签: sql access

我对ms访问的经验很少,我不知道如何让它工作。我通过此查询在标题中收到错误:

SELECT Sum(Sheet2.TOTAL) AS SumOfTOTAL,
       Sheet2.[CUSTOMER NAME],
       Max(Sheet2.[RECORDED DATE]) AS [MaxOfRECORDED DATE],
       Count([FREQUENCY]) AS FREQ,
       Sheet2.TYPE,
       Sheet2.[SALES PERSON INITIAL],
       [TOTAL]/[FREQ] AS [AVE TRANS VALUE]
FROM Sheet2
GROUP BY Sheet2.[CUSTOMER NAME],
         Sheet2.TYPE,
         Sheet2.[SALES PERSON INITIAL]
HAVING (((Sheet2.TYPE)<>"C A N C E L L E D"))
ORDER BY Sum(Sheet2.TOTAL) DESC,
         Sheet2.[CUSTOMER NAME] DESC,
         Max(Sheet2.[RECORDED DATE]) DESC;

请告诉我如何才能运行此查询。

1 个答案:

答案 0 :(得分:0)

Access(以及大多数RDBMS)不允许选择GROUP BY查询中的列,这些列本身不是聚合(例如SUMMAX,或者您有)按列表分组。

问题的根本原因是您在选择列表中使用Sheet2.TOTAL,该列表既不是聚合,也不是出现在列表中。我可以提供以下建议,无论如何,这可能是您正在寻找的:

SELECT SUM(Sheet2.TOTAL) AS SumOfTOTAL,
       Sheet2.[CUSTOMER NAME],
       MAX(Sheet2.[RECORDED DATE]) AS [MaxOfRECORDED DATE],
       COUNT([FREQUENCY]) AS FREQ,
       Sheet2.TYPE,
       Sheet2.[SALES PERSON INITIAL],
       SUM(Sheet2.[TOTAL]) / COUNT([FREQUENCY]) AS [AVE TRANS VALUE]
FROM Sheet2
GROUP BY Sheet2.[CUSTOMER NAME],
         Sheet2.TYPE,
         Sheet2.[SALES PERSON INITIAL]
HAVING (((Sheet2.TYPE)<>"C A N C E L L E D"))
ORDER BY SUM(Sheet2.TOTAL) DESC,
         Sheet2.[CUSTOMER NAME] DESC,
         MAX(Sheet2.[RECORDED DATE]) DESC;