访问查询:iif函数错误

时间:2017-06-27 13:56:01

标签: sql ms-access

我正在尝试列入Answers>列; 8和另一列< 8来自同一领域的答案。[答案(#)]。字段答案。[答案(#)]是1-10级的调查结果。我得到错误"表达式中不能有聚合函数(IIf(答案。[答案(#)]> 8,总和(计数(答案。[答案(#)])),"&# 34))"

在iif函数中,如果我取出sum(查询将分别计算9s和10s,同样适用于8和8以下的数字。我需要在1行中聚合在一起。

SELECT Questions.Year, Questions.[Question Text],
IIf(Answers.[Answer (#)] >8, sum(count(Answers.[Answer (#)])),"") AS Promotors, 
IIf(Answers.[Answer (#)]<=8, sum(Count(Answers.[Answer (#)])),"") AS Detractors
FROM Answers INNER JOIN Questions 
ON Answers.QuestionID = Questions.[Access ID]
WHERE (((Questions.[Question Text]) Like '*Recommend*')) 
and questions.[Question Text] not like '*Are there other technology         services*'
GROUP BY Questions.Year, Questions.[Question Text], Answers.[Answer (#)];

1 个答案:

答案 0 :(得分:1)

我认为你想要条件聚合。像这样:

SELECT Questions.Year, Questions.[Question Text],
       SUM(IIf(Answers.[Answer (#)] > 8, 1, 0) AS Promotors, 
       SUM(IIf(Answers.[Answer (#)] <= 8, 1, 0) AS Detractors
FROM Answers INNER JOIN
     Questions 
     ON Answers.QuestionID = Questions.[Access ID]
WHERE Questions.[Question Text]) Like '*Recommend*' AND
      questions.[Question Text] not like '*Are there other technology         services*'
GROUP BY Questions.Year, Questions.[Question Text];

您无法嵌套聚合函数。您希望条件作为函数的参数,而不是结果。我还从group by删除了答案列。