在Oracle SQL中将数据透视表转换为平面表

时间:2013-09-25 15:47:09

标签: sql sql-server oracle pivot-table

考虑下表:

enter image description here

我正在使用下面的查询(在 SQL Server 中)将此表格转换为平面表格,如下所示: enter image description here

我想使用Oracle SQL做同样的事情。但是,查询在“Oracle SQL”语言中不起作用:cross apply(在下面使用)在Oracle中不起作用。知道如何使用Oracle SQL等效地编写这个吗?谢谢!

select t.employee_id,
   t.employee_name,
   c.data,
   c.old,
   c.new
 from test_table t
 cross apply
 (
   select 'Address', Address_Old, Address_new union all
   select 'Income', cast(income_old as varchar(15)), cast(income_new as varchar(15))
 ) c (data, old, new)

1 个答案:

答案 0 :(得分:1)

如果没有做CROSS APPLY的能力,就不那么简洁了:

select t.employee_id,
t.employee_name,
c.data,
DECODE (c.data, 'Address', t.address_old, 'Income', t.income_old) AS old,
DECODE (c.data, 'Address', t.address_new, 'Income', t.income_new) AS new
from test_table t
cross join
(
  select 'Address' AS data
  from dual
  union all
  select 'Income'
  from dual
 ) c