SQL CASE的THEN语句中有多个值

时间:2011-08-04 17:11:37

标签: sql sql-server tsql case

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

我已经附加了一大块代码,我正在使用它来加入查询中的一些表。我在代码段中添加了评论。

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

你的意思是......?

LEFT JOIN whatever
ON...
CASE...WHEN...THEN...END = 1
OR
CASE...WHEN...THEN...END = 0