将带CASE的SQL转换为Access

时间:2012-07-31 15:11:47

标签: sql ms-access

如何在访问

中完成以下操作
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]) 

2 个答案:

答案 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]);