sum函数中case语句中的SQL语法错误

时间:2014-02-15 21:31:33

标签: sql ms-access-2013

以下是我的代码:

SELECT HOUR(asl.Start_Time) AS [Hour],
       t.team_name,
       SUM( CASE WHEN asl.Working = 1 
                 THEN asl.Duration_Seconds 
                 ELSE 0 END) 
           AS TotalSeconds
FROM (Agents 
         INNER JOIN Teams AS t 
         ON Agents.team_no = Teams.team_no)
      INNER JOIN AgentStateLogs AS asl 
      ON Agents.agent_no = asl.Agent_No
GROUP BY t.team_name, HOUR(asl.Start_Time);

问题在于声明声明中缺少运算符:

SUM( CASE WHEN asl.Working = 1 
          THEN asl.Duration_Seconds 
          ELSE 0 END) 
    AS TotalSeconds

我一直试图找到它而我无法看到错误所在。任何人都可以为我阐明这一点吗?此代码在MS Access 2013中完成。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

CASE WHEN asl.Working = 1 
THEN asl.Duration_Seconds 
ELSE 0 END

转换为JET,这变为

IIf(asl.Working = 1, asl.Duration_Seconds, 0)

the manual中搜索立即以获取更多信息。