【问题标题】:duplicate column during pivot table数据透视表中的重复列
【发布时间】:2016-11-23 12:18:15
【问题描述】:

我有一张这样的桌子

从我的表中选择 *

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

我想像那样旋转我的桌子

从我的数据透视表中选择 *

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?

【问题讨论】:

  • 我在类型栏中有两种类型。我坚持使用 propX 列和类型记录一起使用重复的新列。我还不能在sql中配置它。 @RichBenner
  • 我直接进入了旋转(使用pivot/unpivot 子句),但是当然,简单的条件聚合(参见@Gordon Linoff 的回答)更有效。

标签: sql oracle pivot


【解决方案1】:

我认为条件聚合是一个比复杂枢轴更简单的查询:

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;

【讨论】:

    【解决方案2】:

    如果您的 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

    【讨论】:

    • 这里有什么方法可以跳过 unfivot 步骤吗? @尼古拉斯·克拉斯诺夫
    • @neverwinter 正如我在对您的问题的评论中已经告诉您的那样,是的,当然有一种更有效的方法可以避免完全使用 unpivot, pivot 子句 - 简单的条件聚合(请参阅 Gordon Linoff 的回答)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多