简体   繁体   English

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

[英]SQL - CASE STATEMENT - Count statement and case function

I'm trying to write a case when function within a count statement, but I have a error 4145. 我正在尝试在count语句中编写函数whencase ,但出现错误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

Try this: 尝试这个:

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

Your CASE conditions need to go into the WHERE: 您的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM