sql case语句或if语句

时间:2016-06-30 14:07:28

标签: sql sql-server sql-server-2008

您好我正在做一个简单的SQL报告,无法解决如何做这个简单的case语句。 @risk的参数为1或0。如果存在风险,则返回所有具有风险的记录> 0如果没有风险则返回所有安全记录> 0

SELECT count(b.observationid), a.NumAtRisk , a.CBICode, c.Observerid, d.CBIDescription, d.CBIcatID
FROM dbo.ObservationDetails a
JOIN dbo.Observation b
    ON a.ObservationID = b.ObservationID
join #Temp c 
    on c.Observerid = b.ObserverID
join CBI d
on a.cbiid = d.cbiID
where b.ObservationDate between @Begdate and @Enddate 


-- Parameter needs to return one or the other......................
and case when  
(@risk) = 1  then [a.NumAtRisk] > 0  
     else [a.NumSafe] > 0 

     end    

1 个答案:

答案 0 :(得分:4)

这样的事情:

FlinkKafkaConsumer08

不需要where (@risk = 1 and a.NumAtRisk > 0) or (@risk = 0 and a.NumSafe > 0) 表达式。