Pick another column value depending on column value (postgresql) -


in frontend application have function called pick(value,'col1','col2','col3'). if value 2 value in col2 should picked.

this handsome replacing long code using "case when", "switch case" or "if else" calculations.

i have tried find similar function in postgres, no luck far. seen function array() , values() mentioned, cannot find correct syntax.

the goal set return on of 3 column values depending on first column value.

pseudo code (not working):

select status values(column1,column2,column3)from code 

i know can using "case-when-then-else-end", looking shorter way achieve same thing.

jsfiddle showing principe. want pick 1 value depending on type:

http://sqlfiddle.com/#!15/e0b41/10

you can create array of values pr_* columns, pick 1 of them in way:

(array[prl_1,prl_2,prl_3])[code_type]

here simple demo: http://sqlfiddle.com/#!15/e0b41/23

select *,        (array[prl_1,prl_2,prl_3])[code_type] code left join prl on prl_id =1 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -