如何在访问中完成以下内容
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])
您可以IIF
在这种情况下使用;
AVG( IIF(loc = "1", 1, 0) ) AS locAVG,
假设[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]);