Multiple values in SQL CASE's THEN Statement

不打扰是莪最后的温柔 提交于 2019-12-24 05:04:50

问题


I am wondering if it is possible to specify multiple values in the then part of a case statement in T-SQL?

I have attached a chunk of code where I am using this to join in some tables in a query. I have included a comment in the snippet.

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

回答1:


This is a little ugly, but assuming HeadQuarters is not a decimal/numeric type and only integer values,

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



回答2:


You mean...?

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


来源:https://stackoverflow.com/questions/6945660/multiple-values-in-sql-cases-then-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!