[英]Convert a set of columns into rows in ORACLE SQL
就像您已经知道需要将rows
转换为columns
PIVOT
,类似地将columns
转换为rows
我们需要UNPIVOT
。
由于没有指定列名的特定表,我使用WITH
子句创建了一个临时表来演示。这是了解有关UNPIVOT
使用的条款的更多信息的链接,供您参考。
with table1
as
(
select 'Inbound' department, 50 hour0, 44 hour1, 29 hour2, 47 hour3, 17 hour4 from dual
union all
select 'Outbound', 6, 4, 10, 24, 39 from dual
union all
select 'Returns', 3, 1, 39, 43, 35 from dual
)
select *
from table1 t1
unpivot (value for hour in (hour0 as '0',hour1 as '1',hour2 as '2',hour3 as '3',hour4 as '4'))
在 Oracle 中,我建议使用横向连接来取消旋转表。 假设列名为hour0
、 hour1
、 hour2
、...,这将hour2
为:
select t.department, x.*
from mytable t
cross apply (
select 0 as hour, t.hour0 as value from dual
union all select 1, t.hour1 from dual
union all select 2, t.hour2 from dual
union all ...
) x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.