简体   繁体   English

oracle 将行转换为列

[英]oracle transposing rows to columns

my question is about transposing rows into columns.我的问题是关于将行转换为列。 I have got table T1(c1,c2,c3,c4,c5) columns with datatype varchar2, i want to transpose the rows obtained, example:我有数据类型为 varchar2 的表 T1(c1,c2,c3,c4,c5) 列,我想转置获得的行,例如:

select * from T1

gives

      c1  c2  c3  c4  c5
row1  1   2   3   4   5
row2  A   B   C   D   E
....
rown  U   V   W   X   Y

the result expected is预期的结果是

C1 1 A......U
C2 2 B......V     
C3 3 C......W
C4 4 D......X
C5 5 E......Y

all rows in different columns(table contains only 10-15 rows) i have tried the following query, but it isnt giving expected result.不同列中的所有行(表仅包含 10-15 行)我尝试了以下查询,但没有给出预期的结果。

  Select RN,value 
    From (
           Select x.*,row_number () 
             Over ( Order By c1) rn From T1 x) 
          Unpivot (value For value_type In (C1,c2,c3,c4,c5)
         ); 

So you only need to pivot data again:所以你只需要再次透视数据:

dbfiddle demo dbfiddle 演示

select * 
  from (
      select rn, val, col
        from (select t1.*, row_number() over (order by c1) rn from t1) 
        unpivot (val for col in (c1, c2, c3, c4, c5))) 
  pivot (max(val) for rn in (1, 2, 3, 4)) 
  order by col

You have to know how many rows are in t1 and list them all in pivot in clause (1, 2, 3, 4) alternatively adding aliases for each column.您必须知道t1有多少行,并in子句(1, 2, 3, 4)中的枢轴in列出它们(1, 2, 3, 4)或者为每列添加别名。

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

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