Oracle 11g - 逆透视

Oracle 11g - Unpivot(Oracle 11g - 逆透视)
本文介绍了Oracle 11g - 逆透视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的桌子

Date        Year    Month   Day     Turn_1  Turn_2  Turn_3
28/08/2014  2014    08      28      Foo     Bar     Xab

我想以这样的方式旋转"它:

And i would like to "rotate" it in something like this:

Date        Year    Month   Day     Turn    Source
28/08/2014  2014    08      28      Foo     Turn_1
28/08/2014  2014    08      28      Bar     Turn_2
28/08/2014  2014    08      28      Xab     Turn_3  

我需要源"列,因为我需要将此结果连接到另一个表中:

I need the "Source" column because i need to join this results to another table that say:

Source  Interval
Turn_1  08 - 18
Turn_2  11 - 20
Turn_3  18 - 24

现在我使用 unpivot 来旋转表格,但我不知道如何显示源"列(如果可能的话):

For now i have use unpivot to rotate the table, but i dont know how to display the "Source" column (and if it is possible):

select dt_date, df_year, df_month, df_turn
  from my_rotatation_table 
       unpivot( df_turn
                 for x in(turn_1, 
                          turn_2, 
                          turn_3
              ))

已解决:

select dt_date, df_year, df_month, df_turn, df_source
  from my_rotatation_table 
       unpivot( df_turn
                 for df_source in(turn_1 as 'Turn_1', 
                          turn_2 as 'Turn_2', 
                          turn_3 as 'Turn_3'
              ))

推荐答案

使用这个查询:

with t (Dat, Year, Month, Day, Turn_1, Turn_2, Turn_3) as (
  select sysdate, 2014, 08, 28, 'Foo', 'Bar', 'Xab' from dual
)
select dat, year, month, day, turn, source from t
unpivot (
  source  for turn in (Turn_1, Turn_2, Turn_3)
)

DAT         YEAR    MONTH   DAY TURN    SOURCE
----------------------------------------------
08/01/2014  2014    8       28  TURN_1  Foo
08/01/2014  2014    8       28  TURN_2  Bar
08/01/2014  2014    8       28  TURN_3  Xab

这篇关于Oracle 11g - 逆透视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)