改造一张桌子?使用枢轴/反枢轴

Posted

技术标签:

【中文标题】改造一张桌子?使用枢轴/反枢轴【英文标题】:Transforming a table ? using pivot / unpivot 【发布时间】:2015-01-11 08:07:46 【问题描述】:

我的大脑好痛! 我想转换从下面的 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

.....等等

【问题讨论】:

透视是将组“标题”转换为列并聚合这些列中的值的过程。在某种程度上,这似乎是相反的,尽管您并没有取消聚合。但是你为什么要这样存储数据,而不是以更规范的方式存储数据呢?另外,为什么将数字存储为 varchar? 【参考方案1】:

简单的方法是使用 Unpivot 运算符

select item,
       score,
       unitnum,
from   gcs
       unpivot (score
               for item in(eyes,
                           verbal,
                           motor))

SQLFIDDLE DEMO

【讨论】:

【参考方案2】:

您可以使用简单的联合来获取这些值:

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

【讨论】:

以上是关于改造一张桌子?使用枢轴/反枢轴的主要内容,如果未能解决你的问题,请参考以下文章

嵌套的枢轴列[重复]

Laravel 7 雄辩的嵌套条件,用于使用枢轴进行过滤

是否可以编写具有多个动态枢轴的查询?

不使用 CSS3 围绕左下边缘枢轴旋转图像

枢轴内的窗口功能,可能吗?

用光标+联合+枢轴查询?