oracle中如何将2列1行移动到1列2行

how to move 2 cols 1 row into 1 col 2 rows in oracle

我有一个问题我似乎无法正确回答。我想更改结果集,使其为 1 列并且可以是 N 行。

原始查询

select src_approval, dst_approval
from example_table
where id = 62615
group by src_approval, dst_approval

我尝试了以下方法,但不正确。

select src_approval, dst_approval
from example_table
unpivot
( colvalue for col in (src_approval, dst_approval) )
where id = 62615
group by src_approval, dst_approval

我尝试了以下方法,但仍然有 2 列

select *
from   ( 
select src_approval, dst_approval
from example_table
where id= 62615
group by src_approval, dst_approval
)
 unpivot
       ( income_component_value
         for income_component_type in (src_approval, dst_approval)
       )

结果

select *
from   ( 
select src_approval, dst_approval
from e_p
where exemption_id = 62615
group by src_approval, dst_approval
)
 unpivot
       ( owner
         for approval in (src_approval, dst_approval)
       )

如果您正在考虑unpivot,您可以使用横向连接:

select distinct x.dte
from example_table et cross join lateral
     (select et.src_approval as dte from dual union all
      select et.dst_approval as dte from dual
     ) x
where id = 62615;

更常见的是,这将使用 union:

编写
select et.src_approval
from example_table et
union     -- on purpose to remove duplicates
select et.dst_approval
from example_table et;

是的,您仍然可以使用 unpivot 作为

select colvalue from 
(
select id, colvalue, col
  from example_table   
  unpivot(colvalue for col in(src_approval,dst_approval))
)  
where id = 62615

Demo