有没有办法使用一个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
语句同步?
答案 0 :(得分:2)
没有办法做你所描述的。对于这两种情况,您的案例陈述不仅不同,而且在每种情况下您都会从完全不同的表中返回值。
答案 1 :(得分: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)