1

I have a table of rates and terms from which I need to use the term to select the appropriate rate adjustment. The issue is, each term is its own column as so:

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

I need, for each term, to return the correct adjuster. Thus for 12 months I would need a "2" and for 24 it would be "4" and so on. While vastly oversimplified it captures the essence - I need to select a column name in a table based upon the value of another column in the same table. I'm not able to change the source table. Thanks in advance...

2 Answers 2

4

case is your friend

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

if value of term can be between 12 and 24, etc. then write it this way (I'm not sure what your logic needs to be, but you get the idea)

 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
Sign up to request clarification or add additional context in comments.

Comments

0

What flavor of SQL is that ?

In most of them you can use CASE WHEN (predicate) THEN x statements which you can use to get different columns and then alias it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.