繁体   English   中英

从同一个表中选择多个列而不使用表联合

[英]Select multiple columns from the same table without tables union

我想创建一个查询以从表中选择多个列,并将结果显示为单个列。 我知道我可以使用此查询执行此操作:

select a from Z
union
select b from Z
union 
select c from Z
union 
select d from Z
....

但就我而言,表Z是一个大约50行的子查询,我不想复制和粘贴。 所以我想在Z只出现一次的查询中有这个。 我不知道这是否可行。

你知道这样做的方法吗?

先感谢您。

问候。

从Oracle 11gR1开始,您可以使用unpivot运算符,当然要确保所有列都具有相同的数据类型:

SQL> select val
  2    from (select 1 col1
  3               , 2 col2   
  4               , 3 col3
  5            from dual)
  6  unpivot(
  7    val for col in (col1, col2, col3)
  8  )
  9  ;

       VAL
----------
         1
         2
         3

您可以使用WITH子句来完成它:

with Z as (
    select ... from ... -- <<== Put your big query here
)
select a from Z
union
select b from Z
union 
select c from Z
union 
select d from Z

顶部的with子句使Z可用于查询的其余部分,而不必重复自己。

这是sqlfiddle上的一个演示

暂无
暂无

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

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