Sum Case SQL语法错误

时间:2015-10-14 14:37:58

标签: sql access-vba

我有以下SQL查询:

SELECT 
    CompanyCode, PaymentStatus, PaymentType, PaySource,
    SUM(CCur(PaymentAmount)), 
    SUM(CASE WHEN PaymentStatus='APPROVED' THEN 1 ELSE 0 END) AS Approved,  
    COUNT(*) 
FROM 
    Detail_Work 
GROUP BY 
    CompanyCode, PaymentType, PaymentStatus, PaySource 
ORDER BY 
    CompanyCode, PaymentType, PaymentStatus, PaySource

我收到以下错误:

  

查询中的语法错误(缺少运算符)表达式'SUM(CASE WHEN   PaymentStatus ='已批准'然后1节0结束''

1 个答案:

答案 0 :(得分:1)

MS Access不支持case。请改用iif()

SELECT CompanyCode, PaymentStatus, PaymentType, PaySource,    
       SUM(CCur(PaymentAmount)),
       SUM(IIF(PaymentStatus = 'APPROVED', 1, 0)) AS Approved,  COUNT(*) 
FROM Detail_Work 
GROUP BY CompanyCode,PaymentType, PaymentStatus, PaySource 
ORDER BY CompanyCode, PaymentType, PaymentStatus, PaySource;

但是,我不确定为什么PaymentStatus中有GROUP BY。也许你打算:

SELECT CompanyCode, PaymentType, PaySource,    
       SUM(CCur(PaymentAmount)),
       SUM(IIF(PaymentStatus = 'APPROVED', 1, 0)) AS Approved,  COUNT(*) 
FROM Detail_Work 
GROUP BY CompanyCode,PaymentType, PaySource 
ORDER BY CompanyCode, PaymentType, PaySource;