【问题标题】:Access query: iif function error访问查询:iif函数错误
【发布时间】:2017-06-27 13:56:01
【问题描述】:

我正在尝试从同一字段 Answers.[Answer (#)] 中计算一列 Answers > 8 和另一列 8, sum(count(Answers.[Answer (#)])),""))”。

在 iif 函数中,如果我取出 sum( 查询将分别计算 9s 和 10s,数字 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 (#)];

【问题讨论】:

  • 或许sum(iif(condition, count(...), 0))

标签: sql ms-access


【解决方案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 中删除了答案列。

【讨论】:

  • 在第二个Iif的真实情况下,s1应该是1吗?
  • @CPerkins 。 . .谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多