2

如何在访问中完成以下内容

SELECT MONTH([Date]) as monthNum, 
AVG(CASE WHEN loc ='1' THEN  [Count] END) AS locAVG, 
AVG(CASE WHEN dept ='50' THEN  [Count] END) AS deptAVG, 
FROM [table]
GROUP BY MONTH([Date]) 
4

2 回答 2

5

您可以IIF在这种情况下使用;

AVG( IIF(loc = "1", 1, 0) ) AS locAVG, 
于 2012-07-31T15:16:05.953 回答
1

假设[Count]是一个字段的名称,并且您希望成为其中 的行locAVG的平均值,您可以利用函数忽略 Null 值这一事实。因此,使用表达式提供给when但 Null 否则。[Count]loc ='1'AVG()IIf()[Count]AVG()loc ='1'

对使用类似的IIf()表达式deptAVG

SELECT MONTH([Date]) as monthNum, 
    AVG(IIf(loc ='1', [Count], Null)) AS locAVG, 
    AVG(IIf(dept ='50', [Count], Null)) AS deptAVG, 
FROM [table]
GROUP BY MONTH([Date]); 
于 2012-07-31T15:30:21.287 回答