在数据透视表中重复列

时间:2016-11-23 12:18:15

标签: sql oracle pivot

我有一张像

这样的表格

从myTable中选择*

ID  Type    Prop1    Prop2    Prop3    Prop4    Prop5
--  ------  -------  -------  -------  -------  -------
1   Hot     10       9        23       32       4
1   Cold    2        24       53       34       5
2   Hot     11       9        23       32       4
2   Cold    22       1        53       30       11

我想像我那样转动我的桌子

从myPivotTable中选择*

ID  HotProp1 HotProp2 HotProp3 HotProp4 HotProp5 ColdProp1 ColdProp2 ColdProp3 ColdProp4 ColdProp5 
--  -------  -------  -------  -------  -------- --------- --------- --------- --------- ---------
1   10       9        23       32       4        2          24       53         34       5
2   11       9        23       32       4        22         1        53         30       11

如何在oracle sql中使用 pivot 函数将myTable转换为myPivotTable?

2 个答案:

答案 0 :(得分:2)

我认为条件聚合比复杂的支点更简单:

select id,
       sum(case when type = 'Hot' then Prop1 end) as Hot_Prop1,
       sum(case when type = 'Hot' then Prop2 end) as Hot_Prop2,
       sum(case when type = 'Hot' then Prop3 end) as Hot_Prop3,
       sum(case when type = 'Hot' then Prop4 end) as Hot_Prop4,
       sum(case when type = 'Hot' then Prop5 end) as Hot_Prop5,
       sum(case when type = 'Cold' then Prop1 end) as Cold_Prop1,
       sum(case when type = 'Cold' then Prop2 end) as Cold_Prop2,
       sum(case when type = 'Cold' then Prop3 end) as Cold_Prop3,
       sum(case when type = 'Cold' then Prop4 end) as Cold_Prop4,
       sum(case when type = 'Cold' then Prop5 end) as Cold_Prop5
from myPivotTable
group by id;

答案 1 :(得分:1)

如果您使用的是Oracle 11g及更高版本,您只需“重新转动”它(您已经转动了数据) - 首先取消它,然后再转动:

with t1(id1, type1, Prop1, Prop2, Prop3, Prop4, Prop5) as(
  select 1,   'Hot' ,  10 ,  9 , 23,32 , 4 from dual union all
  select 1,   'Cold',  2  ,  24, 53,34 , 5  from dual union all
  select 2,   'Hot' ,  11 ,  9 , 23,32 , 4  from dual union all
  select 2,   'Cold',  22 ,  1 , 53,30 , 11 from dual 
  )
 select *
   from( select *
           from t1
        unpivot (
          val for col in (prop1, prop2, prop3, prop4, prop5)
        )
   )
   pivot(
     max(val) for (col, type1) in (('PROP1', 'Hot') as HotProp1, 
                                   ('PROP2', 'Hot') as HotProp2, 
                                   ('PROP3', 'Hot') as HotProp3,
                                   ('PROP4', 'Hot') as HotProp4,
                                   ('PROP5', 'Hot') as HotProp5,
                                   ('PROP1', 'Cold') as ColdProp1,
                                   ('PROP2', 'Cold') as ColdProp2,
                                   ('PROP3', 'Cold') as ColdProp3,
                                   ('PROP4', 'Cold') as ColdProp4,
                                   ('PROP5', 'Cold') as ColdProp5)
   ) 

结果:

      ID1   HOTPROP1   HOTPROP2   HOTPROP3   HOTPROP4   HOTPROP5  COLDPROP1  COLDPROP2  COLDPROP3  COLDPROP4  COLDPROP5
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
         1         10          9         23         32          4          2         24         53         34          5
         2         11          9         23         32          4         22          1         53         30         11

Here is the demo