2009-11-03 125 views
0

有没有办法使用一个CASE语句并返回2个不同的值?TSQL - 如何用一个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语句同步。

回答

2

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

+0

@Josheph。谢谢你的答案。这个问题主要是为了确认是否真的有办法这样做。但即使在阅读MSDN BOL之后,我仍然无法找到解决方法。 – Sung 2009-11-03 16:59:23

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)