如何在Oracle 11g中将动态列转换为行?

时间:2018-07-12 10:10:09

标签: sql oracle oracle11g

我正在尝试将动态列转换为Oracle 11g中的行。我知道可以使用unpivot,但是只有预先存在的列而不是动态列。

数据输入:

select 'Bulding1', 'Building2', 'Building3', 'Building4', 'Building5','Building6' 
from dual;

预期输出:

Column_Name
-----------
- Building1
- Building2
- Building3
- Building4
- Building5
- Building6
- Building7

1 个答案:

答案 0 :(得分:1)

使用UNION ALL

SELECT 'Building1' AS column_name FROM DUAL
UNION ALL
SELECT 'Building2' FROM DUAL
UNION ALL
SELECT 'Building3' FROM DUAL
UNION ALL
SELECT 'Building4' FROM DUAL
UNION ALL
SELECT 'Building5' FROM DUAL
UNION ALL
SELECT 'Building6' FROM DUAL;

或使用UNPIVOT

SELECT column_name
FROM   (
  SELECT 'Building1',
         'Building2',
         'Building3',
         'Building4',
         'Building5',
         'Building6' 
  FROM   DUAL
)
UNPIVOT(
  column_name FOR name IN (
    "'BUILDING1'",
    "'BUILDING2'",
    "'BUILDING3'",
    "'BUILDING4'",
    "'BUILDING5'",
    "'BUILDING6'"
  )
);