“as”附近的案例表达式语法错误

时间:2013-01-18 21:56:08

标签: sql syntax case

我的案例表达式出错了。我只想评估一个条件,并返回正确的语句。但我一直在“as”附近收到语法错误。

这就是我所拥有的:

left outer join (            
        SELECT wbs1, wbs2, wbs3  
    , case 
            when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
            then '[AEP] as ''AEP1'''
            else [AEP Base] as 'AEP1'
        end
    , [AEP:Non-Compliant Mechanical Ventilation] as 'AEP2'  
    , [AEP - Non Energy Star AC ($90 deduction)] as 'AEP3'  
    , [AEP: Bonus] as 'AEP4'

2 个答案:

答案 0 :(得分:4)

您的CASE语法错误。你有一个用单引号括起来的列名,你有两个地方的别名(都错了):

 , case 
    when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
    then '[AEP] as ''AEP1'''  -- <-- don't put the column in single quotes and no alias here
    else [AEP Base] as 'AEP1'  -- < don't put the alias here
   end  -- < the alias goes here

所以你的CASE应该是:

, case 
    when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
    then [AEP]
    else [AEP Base] 
    end as AEP1  

别名位于END表达式中的CASE之后。

答案 1 :(得分:2)

end之前case中应该as

case 
  when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) then [AEP] as 
  else [AEP Base] 
end as [AEP1]
// not else [AEP Base]  as 'AEP1' end

更新