sql case语句或if语句

时间:2022-10-01 22:26:06

Hi I am doing a simple sql report and can not wrap my head around how to do this simple case statement . a parameter is either 1 or 0 for @risk. If there is a risk then return all of the records with risks > 0 if there is no risk then return all the safe records > 0

嗨,我正在做一个简单的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 个解决方案

#1


4  

Something like this:

像这样的东西:

where (@risk = 1 and a.NumAtRisk > 0) or
      (@risk = 0 and a.NumSafe > 0)

A case expression is not needed.

不需要案例表达式。

#1


4  

Something like this:

像这样的东西:

where (@risk = 1 and a.NumAtRisk > 0) or
      (@risk = 0 and a.NumSafe > 0)

A case expression is not needed.

不需要案例表达式。