我的大脑受伤了! 我想转换从下面的sql生成的表中的数据。 使用数据透视的示例往往涉及汇总数据。显然,我实际上正在扩展数据。
CREATE TABLE GCS
(
EYES VARCHAR2(20)
, VERBAL VARCHAR2(20)
, MOTOR VARCHAR2(20)
, UNITNUM VARCHAR2(20)
);
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140560');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('3',4,'2','140729');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('1',2,'6','140771');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140502');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',4,'6','140537');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140566');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140571');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140781');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140780');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('3',4,'5','140788');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140585');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140577');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140747');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140778');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140569');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',1,'6','140575');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140779');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140785');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140753');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140786');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140555');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140557');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140554');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140736');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',4,'6','140745');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140783');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140556');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140559');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140574');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140573');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140572');
Insert into GCS ("Eyes","Verbal","Motor",UNITNUM) values ('4',5,'6','140738');
commit;
进入以下表格。我确信我需要使用pivot / unpivot,但无法解决问题。
Item | Score | Unitnum
----------
Eyes 4 140560
Verbal 5 140560
Motor 6 140560
Eyes 3 140729
Verbal 4 140729
Motor 2 140729
Eyes 1 140771
Verbal 2 140771
Motor 6 140771
.....等
答案 0 :(得分:1)
您可以使用简单的联合来获取这些值:
select 'Eyes' as Item, Eyes, UnitNum
from GCS
union all
select 'Verbal' as Item, Verbal, UnitNum
from GCS
union all
select 'Motor' as Item, Motor, UnitNum
from GCS
如果需要,您可以按这些列进行订购,包括刚刚介绍的“项目”:
select 'Eyes' as Item, Eyes, UnitNum
from GCS
union all
select 'Verbal' as Item, Verbal, UnitNum
from GCS
union all
select 'Motor' as Item, Motor, UnitNum
from GCS
order by
unitnum, item
请参阅SQLFiddle
答案 1 :(得分:1)
简单的方法是使用 Unpivot
运算符
select item,
score,
unitnum,
from gcs
unpivot (score
for item in(eyes,
verbal,
motor))