开发者

TSQL - How to return 2 values with one case statement?

开发者 https://www.devze.com 2022-12-10 07:28 出处:网络
Is there a way to use one CASE statement and return 2 different values? Following query has 2 CASE statements with same conditions.

Is there a way to use one CASE statement and return 2 different values?

Following query has 2 CASE statements with same conditions.

select  case when DA.value = 1 开发者_运维百科
            then da.Good else da.Bad end as Foo,
        case when DA.value = 1 
            then ot.Good else ot.Bad end as Bar,
from    someTable DA (nolock)
        join otherTable OT (nolock) on OT...
where   ...

Is there anyway, to specify that CASE statement once?

so that there is no need to keep both CASE statements in sync whenever the condition changes?


There's no way to do what you're describing. Not only is your case statement different for both cases, but you're returning values from totally different tables in each case.


Not sure if this is what you need, but you can do combinations like:

select
   case
      when da.value = 1 and ot.attributeValue = 1 then ot.good
      when da.value = 2 and ot.attributeValue = 3 then ot.good
      ...
      else ot.bad
   end Result
from
   someTable DA

   JOIN otherTable OT
      on (ot.id = da.id)
0

精彩评论

暂无评论...
验证码 换一张
取 消