0

有没有办法使用一个CASE语句并返回 2 个不同的值?

以下查询有 2 个CASE条件相同的语句。

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   ...

无论如何,要指定该CASE语句一次吗?
这样就不需要CASE在条件发生变化时使两个语句保持同步?

4

2 回答 2

2

没有办法做你所描述的。不仅两种情况下的 case 语句不同,而且在每种情况下,您都从完全不同的表中返回值。

于 2009-11-03T16:21:49.230 回答
2

不确定这是否是您需要的,但您可以执行以下组合:

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)
于 2009-11-03T16:36:18.790 回答