SQL中的CASE语句不起作用

时间:2018-08-21 19:13:56

标签: sql sql-server

我在下面的代码中遇到错误,尽管正在寻找答案,但还是无法弄清楚。

错误:

  

关键字“ CASE”附近的语法不正确

我的代码:

SELECT     
    PS_Margin.[Project Profit by Person %], 
    PS_Long.[Days Between], PS_Long.[Entered Hours], 
    PS_Long.[Maximum Month Worked], PS_Long.[Minimum Month Worked], 
    PS_Long.[Project ID], PS_Proj.[Project Name], 
    ((PS_Util_Sum.[Current Month Forecast Hours] + PS_Util_Sum.[Next Month Forecast Hours] + PS_Util_Sum.[Next Monthplus1 Forecast Hours])/
    (PS_Util_Sum.[Current Month Adj Avail Hours] + PS_Util_Sum.[Next Month Adj Avail Hours] + PS_Util_Sum.[Next Monthplus1 Adj Avail Hours])) AS [90 Day Forecast Percent],
    ((PS_Util_Sum.[Current Month Goal Hours] + PS_Util_Sum.[Next Month Goal Hours] + PS_Util_Sum.[Next Monthplus1 Goal Hours])/
    (PS_Util_Sum.[Current Month Adj Avail Hours] + PS_Util_Sum.[Next Month Adj Avail Hours] + PS_Util_Sum.[Next Monthplus1 Adj Avail Hours])) AS [90 Day Goal Percent],
    (PS_Util_Sum.[Current Month Forecast Hours] + PS_Util_Sum.[Next Month Forecast Hours] + PS_Util_Sum.[Next Monthplus1 Forecast Hours]) AS [90 Day Projected Production],
    PS_Util_Sum.[PYTD Charged Hours] / PS_Util_Sum.[PYTD Adj Avail Hours] AS [PYTD Utilization],
    PS_Util_Sum.[PYTD Goal Hours] / PS_Util_Sum.[PYTD Adj Avail Hours] AS [PYTD Utilization Goal],
    [PYTD Utilization] - [PYTD Utilization Goal] AS [PYTD Var to Goal],
    [90 Day Goal Percent] - [90 Day Forecast Percent] AS [90Day Var]
    (CASE 
        WHEN PS_Util_Sum[PYTD Var to Goal %]> -.1 AND PS_Util_Sum[90Day Var] >= 0  
           THEN 1
        WHEN PS_Util_Sum[PYTD Var to Goal %]<= -.1 AND PS_Util_Sum[90Day Var] >= 0 
           THEN 2
        WHEN PS_Util_Sum[PYTD Var to Goal %]> -.1 AND PS_Util_Sum[90Day Var]< 0 
           THEN 3
        ELSE 4 
    END) AS [Quad Group]
FROM         
    dbo.PS_Emp 
LEFT OUTER JOIN 
    dbo.PS_Margin ON PS_Emp.[USID] = PS_Margin.[Emp or Vendor ID] 
LEFT OUTER JOIN 
    dbo.PS_Util_Sum ON PS_Emp.[USID] = PS_Util_Sum.[Employee_ID] 
LEFT OUTER JOIN 
    dbo.PS_Long ON PS_Emp.[USID] = PS_Long.[Employee ID]
INNER JOIN 
    dbo.PS_Proj ON PS_Long.[Project ID] = PS_Proj.[Project ID]

1 个答案:

答案 0 :(得分:1)

印刷错误。

[90 Day Goal Percent] - [90 Day Forecast Percent] AS [90Day Var]更改为[90 Day Goal Percent] - [90 Day Forecast Percent] AS [90Day Var],(末尾缺少逗号)。