SQL-CASE STATEMENT-Count语句和大小写功能

时间:2019-01-08 13:02:17

标签: sql tsql case

我正在尝试在count语句中编写一个case when函数,但出现错误4145。

select @CountResult = count(*) from ViewDefinition where DefinitionID = @ObjektID and
    (case 
         when IsGepa=0 and Gepa is not null then @CountResult  
         when NotGepa=0 and GepaListeID is not null then @CountResult  
    end  )

select @CountResult

2 个答案:

答案 0 :(得分:0)

尝试一下:

select @CountResult = SUM
(
    CASE when (IsGepa=0 and Gepa is not null ) OR (NotGepa=0 and GepaListeID is not null) THEN 1 ELSE 0 END
)
from ViewDefinition 
where DefinitionID = @ObjektID

答案 1 :(得分:0)

您的CASE条件需要输入到哪里:

select @CountResult = count(*) 
from ViewDefinition 
where DefinitionID = @ObjektID and
  ((IsGepa=0 and Gepa is not null) or  
   (NotGepa=0 and GepaListeID is not null)  
  )

select @CountResult