在Access中使用IIF给出了聚合函数错误

时间:2013-01-13 15:35:15

标签: sql ms-access aggregate-functions iif

SELECT Bot.BetType, 
   Sum(Bot.Result) AS Won, 
   IIf([Bot]![Market Name] Like "*Place*", "Place", "Win") AS Type
FROM Bot
GROUP BY Bot.BetType, Type;

获取错误:

  

您尝试执行的查询不包含指定的表达式If([Bot]![Market Name] Like "*Place*", "Place", "Win")作为聚合函数的一部分。

我没有找到谷歌的结果。如果您有任何疑问,请随时提出。

2 个答案:

答案 0 :(得分:3)

您不能在GROUP BY中使用别名:

SELECT Bot.BetType, 
   Sum(Bot.Result) AS Won, 
   IIf([Bot]![Market Name] Like "*Place*", "Place", "Win") AS Type
FROM Bot
GROUP BY Bot.BetType, IIf([Bot]![Market Name] Like "*Place*", "Place", "Win");

答案 1 :(得分:1)

如果您使用子查询,我认为您可以使用别名:

SELECT B.BetType, 
       Sum(B.Result) AS Won,
       type
from (select b.*, IIf([Bot]![Market Name] Like "*Place*", "Place", "Win") AS Type
      FROM Bot b
     ) b
GROUP BY b.BetType, Type;