简体   繁体   English

取消表Oracle中的数据

[英]Unpivot data in table Oracle

My source is like 我的来源就像

**Col1  Col2 Col3  Col4    Col5  Col6**
1     ABC  STAT1  STAT2  COM1  COM2

How do I get below 我如何到达下方

**Col1 Col2 type Col4   Col5**
1    ABC  STAT STAT1  STAT2
1    ABC  COM  COM1   COM2

I looked at Unpivot, its able to do one column -> one row . 我看着Unpivot,它能够执行一列->一行。 But I'm looking for 2 columns into 1 row. 但是我正在寻找2列成1行。 Union all would be one option, but I would like to see it using unpivot 全部合并将是一个选择,但我希望使用unpivot看到它

My preferred method is a lateral join, but that is not available until Oracle 12c. 我首选的方法是横向联接,但是直到Oracle 12c才可用。 If you have only two options and your table is not really big, union all is probably sufficient: 如果只有两个选项并且表不是很大,则union all合并就足够了:

select col1, col2, 'STAT' as type, col3 as col4, col4 as col5
from t
union all
select col1, col2, 'COM' as type, col5, col6
from t;

If scanning the table twice is a performance issue, there are other options as well. 如果扫描表两次是性能问题,则还有其他选项。

A pretty simple way without unpivot is a join : 没有一个非常简单的方式unpivot是一个join

select t.col1, t.col2, tt.type,
       (case when tt.type = 'STAT' then col3 else col5 end),
       (case when tt.type = 'STAT' then col4 else col6 end)
from t cross join
     (select 'STAT' as type from dual union all
      select 'COM' as type from dual
     ) tt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM