在oracle查询中选择行值作为列

时间:2017-12-19 06:54:07

标签: sql oracle

这是表格:

select * from ABCD;

|    ABCD_ID | ROLE  | NAME | PARAM   | VALUE |
+============+=======+======+=========+=======+
|    1       | Allow | A1   | Period1 | 1     |
|    1       | Allow | A1   | Period1 | 2     |
|    1       | Allow | A1   | Period1 | 3     |
|    2       | Allow | A2   | Period2 | 11    |
|    2       | Allow | A2   | Period2 | 12    |
|    3       | Allow | A3   | Period3 | 111   |
|    4       | Allow | A4   | XY      | 200   |

我想运行select query以将句点作为列,列的值将为" VALUE"

需要输出:

|    ABCD_ID   | ROLE  | NAME | Period |
+==============+=======+======+========+
|            1 | Allow | A1   | 1      |
|            1 | Allow | A1   | 2      |
|            1 | Allow | A1   | 3      |
|            2 | Allow | A2   | 11     |
|            2 | Allow | A2   | 12     |
|            3 | Allow | A3   | 111    |

我试过查询:

select ABCD_ID,role,name,param, 
       (select distinct value 
        from ABCD 
        where ABCD_ID=a.ABCD_ID 
          and param like '%Period%') as "Period" 
from ABCD a 
where role='Allow' 
  and param like '%Period%'

获取此错误: ORA-01427:单行子查询返回多行

2 个答案:

答案 0 :(得分:1)

对我来说看起来像一个简单的列别名:

select ABCD_ID,
       role,
       name,
       value as "Period"
from ABCD a 
where role='Allow' 
  and param like 'Period%'

答案 1 :(得分:0)

我认为你不需要添加' role ='允许'

的条件。

选择ABCD_ID,        角色,        名称,        价值为"期间" 来自ABCD a 其中param类似于' Period%'