一尘不染

SQL CASE的THEN语句中的多个值

sql

我想知道是否可以在case语句的then部分中指定多个值T-SQL

我已经在使用此代码的地方附加了一大段代码,以加入查询中的某些表中。我已在代码段中添加了评论。

LEFT JOIN Business B ON v.BusID = B.BusID
LEFT JOIN BusinessTypeKey T ON B.BusinessTypeID = T.BusTypeID
LEFT JOIN Location L ON L.BusID = B.BusID
AND L.HeadQuarters = CASE 
WHEN (SELECT COUNT(1) from Location L2
WHERE L2.BusID = B.BusID) = 1                                                                           
THEN 1,0   -- Would like to specify either 1 or 0 here. I suppose I could also make it euqal to -> L.HeadQuarters but would like a better way to impose it                                                                                                  
ELSE 1  
END

阅读 176

收藏
2021-03-08

共1个答案

一尘不染

这有点丑陋,但假设HeadQuarters不是十进制/数字类型,而只是整数值,

AND L.HeadQuarters BETWEEN CASE WHEN (SELECT COUNT...) = 1 THEN 0 ELSE 1 END AND 1;
2021-03-08