2015-10-14 111 views
0

我有以下SQL查詢:總和案例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 

而且我得到以下錯誤:

Syntax Error (Missing Operator) in query Expression 'SUM(CASE WHEN PaymentStatus='APPROVED' THEN 1 ELSE 0 END)'

+0

PaymentStatus是批准或拒絕。 – GhostDZ9

回答

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; 

不過,我不知道爲什麼你在GROUP BYPaymentStatus。也許你打算:

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; 
+0

工作感謝一堆! – GhostDZ9