Is there a way to use one CASE
statement and return 2 different values?
是否有一种方法可以使用一个CASE语句并返回两个不同的值?
Following query has 2 CASE
statements with same conditions.
下面的查询有两个具有相同条件的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 ...
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?
是否有,一次指定CASE语句?因此,当条件发生变化时,就不需要同时保持两个CASE语句的同步?
2 个解决方案
#1
2
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.
没有办法做到你所描述的。不仅两种情况下的case语句不同,而且在每种情况下都返回来自完全不同表的值。
#2
2
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)
#1
2
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.
没有办法做到你所描述的。不仅两种情况下的case语句不同,而且在每种情况下都返回来自完全不同表的值。
#2
2
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)