将两列合二为一

时间:2016-04-17 11:59:37

标签: database oracle oracle11g

我有像

这样的表格

Tabel data
+-------+-------+
| name1 | name2 |
+-------+-------+
| AA    | XX    |
| BB    | YY    |
| CC    | ZZ    |
+-------+-------+

我希望输出像

Edit
+-------+
| name1 |
+-------+
| AA    |
| XX    |
| BB    |
| YY    |
| CC    |
| ZZ    |
+-------+

请您指导我查询以获取此任务

1 个答案:

答案 0 :(得分:2)

您可以使用UNION ALLUNIONUNION将创建DISTINCT结果集。如果您不想要,可以使用UNION ALL,这将简单地连接数据

SELECT NAME1 as NAME FROM YOUR_TABLE
UNION
SELECT NAME2 FROM YOUR_TABLE
ORDER BY NAME

编辑:了解更新后的要求

with my_data(name1,name2) as
(
  select 'AA','XX' from dual
  union
  select 'BB','YY' from dual
  union
  select 'CC','ZZ' from dual
),
indexed_my_data(name1,name2,row_sequence) as
(
  select name1,name2, row_number() over (order by null) from my_data
)
select name1,row_sequence,'A' as col_sequence from indexed_my_data
union all
select name2,row_sequence,'B' from indexed_my_data
ORDER BY row_sequence,col_sequence;