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

Sql 如何在oracle中将2列1行移动到1列2行,sql,oracle,unpivot,Sql,Oracle,Unpivot,我有一个问题,我似乎没有弄对。我想更改结果集,使其为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_ap

我有一个问题,我似乎没有弄对。我想更改结果集,使其为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
我尝试了下面的方法,仍然有两列

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)
       )

如果您正在考虑取消PIVOT,则可以使用横向连接:

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;
更常见的情况是,只需使用
联合

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

如果您正在考虑取消PIVOT,则可以使用横向连接:

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;
更常见的情况是,只需使用
联合

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

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

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

样本数据和期望的结果会很有帮助。样本数据和期望的结果会很有帮助。哪一个会更快,因为我会有数百万行。横向联接不会返回任何结果results@shorif2000 . . . 它是否返回错误?否则,
where
子句会过滤掉所有的值。这会更快,因为我会有数百万行。横向连接不会返回任何值results@shorif2000 . . . 它是否返回错误?否则,
where
子句会过滤掉所有的值。您所拥有的和我粘贴的结果之间有什么区别?在unpivot的第一个例子中,列来自unpivot关键字(id、colvalue、col)后面的parantesed部分,而不是表中的列。事实上,第二个应该可以@shorif2000使用。您所拥有的和我粘贴的结果之间有什么区别?在unpivot的第一个例子中,列来自unpivot关键字(id、colvalue、col)后面的parantesed部分,但不是表中的列。事实上,第二个应该在@shorif2000起作用。