数据透视 table 期间的重复列
duplicate column during pivot table
我有一个table喜欢
select * 来自 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
我想像那样转动我的 table
select * 来自 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?
如果您的 Oracle 版本 11g 及更高版本,您可以简单地 "re-pivot" 它(您已经旋转了数据)- 先取消旋转然后再次旋转:
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
我认为条件聚合是一个比复杂数据透视表简单得多的查询:
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;
我有一个table喜欢
select * 来自 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
我想像那样转动我的 table
select * 来自 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?
如果您的 Oracle 版本 11g 及更高版本,您可以简单地 "re-pivot" 它(您已经旋转了数据)- 先取消旋转然后再次旋转:
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
我认为条件聚合是一个比复杂数据透视表简单得多的查询:
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;