我有一个费率和条款表,我需要使用该术语来选择适当的费率调整。问题是,每个术语都是它自己的专栏:
term 12mon 24mon 36mon
----- ----- ----- -----
12 2 4 6
24 2 4 6
对于每个学期,我需要返回正确的调节器。 因此,12个月我需要一个“2”,而24岁则需要“4”,依此类推。虽然它过于简单,但它捕获了本质 - 我需要根据同一个表中另一列的值在表中选择一个列名。 我无法更改源表。 提前谢谢......
答案 0 :(得分:4)
案例是你的朋友
case term
when 12 then [12mon]
when 24 then [24mon]
when 36 then [36 mon]
end as rate
如果术语的值可以在12到24之间,等等,那就这样写(我不确定你的逻辑需要什么,但是你明白了)
case
when term < 12 then 0
when term < 24 then [12mon]
when term < 36 then [24mon]
when term < 48 then [36mon]
else [48mon]
end as rate
答案 1 :(得分:0)
那是什么样的SQL?
在大多数情况下,您可以使用CASE WHEN (predicate) THEN x
语句来获取不同的列,然后对其进行别名。