一、列转行:用到unpivot
创建测试表:
CREATE TABLE "NBBUSITS_VTRON"."TT"
( "ID" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"NAME" VARCHAR2(20 BYTE),
"DATA1" NUMBER,
"DATA2" NUMBER
)
插入测试数据:
Insert into TT (ID,NAME,DATA1,DATA2) values ('1','包一',13.6,222.3);
Insert into TT (ID,NAME,DATA1,DATA2) values ('2','陈二',14,1123);
Insert into TT (ID,NAME,DATA1,DATA2) values ('3','张三',32,34123);
Insert into TT (ID,NAME,DATA1,DATA2) values ('4','李四',46,4);
Insert into TT (ID,NAME,DATA1,DATA2) values ('5','唐五',776,12);
Insert into TT (ID,NAME,DATA1,DATA2) values ('6','郑六',4,24);
Insert into TT (ID,NAME,DATA1,DATA2) values ('7','刘七',6,41);
Insert into TT (ID,NAME,DATA1,DATA2) values ('8','王八',56,4312);
Insert into TT (ID,NAME,DATA1,DATA2) values ('9','屠九',57,214);
Insert into TT (ID,NAME,DATA1,DATA2) values ('10','释十',76,123);
表数据长这样:
列转行后效果:
二、行转列:用到pivot
创建测试表:
CREATE TABLE "NBBUSITS_VTRON"."TS"
( "ID" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"NAME" VARCHAR2(20 BYTE),
"DATA" NUMBER
)
插入数据:
Insert into TS (ID,NAME,DATA) values ('1','张三',123);
Insert into TS (ID,NAME,DATA) values ('2','张三',321);
Insert into TS (ID,NAME,DATA) values ('3','李四',453);
Insert into TS (ID,NAME,DATA) values ('4','李四',654);
Insert into TS (ID,NAME,DATA) values ('5','唐五',125);
Insert into TS (ID,NAME,DATA) values ('6','唐五',645);
Insert into TS (ID,NAME,DATA) values ('7','郑六',765);
Insert into TS (ID,NAME,DATA) values ('88','郑六',345);
表数据长这样:
行转列后效果: