我有像
这样的表格Tabel data +-------+-------+ | name1 | name2 | +-------+-------+ | AA | XX | | BB | YY | | CC | ZZ | +-------+-------+
我希望输出像
Edit +-------+ | name1 | +-------+ | AA | | XX | | BB | | YY | | CC | | ZZ | +-------+
请您指导我查询以获取此任务
答案 0 :(得分:2)
您可以使用UNION ALL
或UNION
。 UNION
将创建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;