1

我有一个费率和条款表,我需要从中使用该术语来选择适当的费率调整。问题是,每个术语都是它自己的列,因此:

term  12mon  24mon  36mon
----- -----  -----  -----
 12     2      4      6
 24     2      4      6

对于每个学期,我需要返回正确的调整器。因此,在 12 个月内,我需要一个“2”,而对于 24 个月,我需要一个“4”,依此类推。虽然过于简单,但它抓住了本质——我需要根据同一个表中另一列的值在表中选择一个列名。我无法更改源表。提前致谢...

4

2 回答 2

4

案例是你的朋友

 case term 
    when 12 then [12mon] 
    when 24 then [24mon] 
    when 36 then [36 mon] 
 end as rate

如果 term 的值可以在 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
于 2013-08-02T23:30:54.410 回答
0

那是什么味道的 SQL?

在大多数情况下,您可以使用CASE WHEN (predicate) THEN x语句来获取不同的列,然后为其设置别名。

于 2013-08-02T23:31:58.443 回答