我的SQL查询出了什么问题?语法错误

时间:2010-10-12 16:14:42

标签: sql

这个SQL查询工作正常,直到我尝试添加CASE语句,但现在我得到语法错误,如本文底部所述。这是SQL(非常史诗,但语法错误发生在CASE周围,可能是先前的SageAccount选择)

SELECT CurrentBalance.Value AS CurrentBalanceValue, 
       Debt30Balance.Value AS Debt30BalanceValue, 
       Debt60Balance.Value AS Debt60BalanceValue, 
       Debt90Balance.Value AS Debt90BalanceValue, 
       WIP.Value AS WIPValue, CurrentBalance.Value + Debt30Balance.Value + 
           Debt60Balance.Value + Debt90Balance.Value AS SLDebt,  
       CurrentBalance.Value + Debt30Balance.Value + Debt60Balance.Value + 
           Debt90Balance.Value+ WIP.Value AS TotalDebt,
       (SELECT CreditTerms FROM SageAccount WHERE ID=@CustomerID) 
       AS CreditTerms,
CASE WHEN CreditTerms <= 30 THEN
CurrentBalance.Value + Debt30Balance.Value + 
    Debt60Balance.Value + Dept90Balance.Value
ELSE WHEN CreditTerms <= 60 THEN
Debt30Balance.Value + Debt60Balance.Value + Dept90Balance.Value
ELSE WHEN CreditTerms <= 90 THEN
Debt60Balance.Value + Dept90Balance.Value
ELSE
Debt90Balance.Value
END AS Overdue
FROM (SELECT TOP (1) Value, Customer FROM DebtBreakdown 
       WHERE (Type = 0) AND (Customer = @CustomerID) 
       ORDER BY Timestamp DESC) AS CurrentBalance 
       INNER JOIN (SELECT TOP (1) Value, Customer 
       FROM DebtBreakdown AS DebtBreakdown_1 
       WHERE (Type = 1) AND (Customer = @CustomerID) 
       ORDER BY Timestamp DESC) AS Debt30Balance ON 
       CurrentBalance.Customer = Debt30Balance.Customer 
       INNER JOIN (SELECT TOP (1) Value, Customer FROM 
       DebtBreakdown AS DebtBreakdown_2 
       WHERE (Type = 2) AND (Customer = @CustomerID) 
       ORDER BY Timestamp DESC) 
       AS Debt60Balance ON Debt30Balance.Customer = Debt60Balance.Customer 
       INNER JOIN (SELECT TOP (1) Value, Customer FROM DebtBreakdown AS 
       DebtBreakdown_3 WHERE (Type = 3) AND (Customer = @CustomerID) 
       ORDER BY Timestamp DESC) AS Debt90Balance ON 
       Debt60Balance.Customer = Debt90Balance.Customer 
       INNER JOIN (SELECT TOP (1) Value, Customer FROM DebtBreakdown AS 
       DebtBreakdown_4 WHERE (Type = 4) AND (Customer = @CustomerID) 
       ORDER BY Timestamp DESC) AS WIP ON Debt90Balance.Customer = WIP.Customer

抱歉,忘记了错误!

'WHEN'附近的WHERE子句出错。 WHERE子句在','附近出错。 “ORDER”附近的WHERE子句出错。无法解析查询文本。

1 个答案:

答案 0 :(得分:10)

尝试将“ELSE WHEN”语句更改为“WHEN”(将最终ELSE保留为原样)