如何在访问
中完成以下操作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])
答案 0 :(得分:5)
在这种情况下,您可以使用IIF
;
AVG( IIF(loc = "1", 1, 0) ) AS locAVG,
答案 1 :(得分:1)
假设[Count]
是字段的名称,并且您希望locAVG
为[Count]
行的loc ='1'
的平均值,则可以利用这一事实AVG()
函数忽略Null值。因此,在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]);