简体   繁体   English

将列名的文本值转换为查询中的列

[英]Convert Text value of column name to Column in a query

I am trying to dynamically fetch the value of column named 'abc' using the text that is deduced from the decode function.我正在尝试使用从解码 function 推导出的文本动态获取名为“abc”的列的值。 Decode function is returning the text value of the column, that is 'abc' but not the underlying value dynamically.解码 function 正在返回列的文本值,即“abc”,但不是动态的基础值。 Is there a way to do this?有没有办法做到这一点?

SELECT LOWER(DECODE('METRIC','METRIC','abc','MODEL','xyz','COUNTRY','US')) as abc FROM table_test

Expected result:预期结果:

abc
---
1.2 
1.5
1.6
1.7

table_test:表测试:

在此处输入图像描述

If you can't use columns as the values in DECODE , you may always write a regular CASE expression:如果你不能使用列作为DECODE中的值,你可能总是写一个正则CASE表达式:

SELECT
    CASE metric WHEN 'METRIC'  THEN abc
                WHEN 'MODEL'   THEN xyz
                WHEN 'COUNTRY' THEN US END AS output
FROM table_test;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM