oracle pivot / unpivot
发布时间:2021-01-18 06:04:19 所属栏目:站长百科 来源:网络整理
导读:1.pivot 行转列 ? ?pivot 用法: select ... from ...pivot -- 留意:在from 与where 之间的要害字 (pivot_clause pivot_for_clause pivot_in_clause) where ...( 1 )pivot_clause:界说要举办聚积的列;( 2 )pivot_for_clause:界说要分组和转置的列;( 3 )pi
|
1.pivot 行转列 ? ?pivot 用法: select ...
from ...
pivot --> 留意:在from 与where 之间的要害字
(pivot_clause
pivot_for_clause
pivot_in_clause)
where ...
(1)pivot_clause:界说要举办聚积的列;
(2)pivot_for_clause:界说要分组和转置的列;
(3)pivot_in_clause:界说限制功效的值的范畴。发生的每个值的聚积转换为单唯一列。
? ?pivot 留意事项: ? ?(1).任何仅在pivot子句中引用的列,不能用在select 列表中; ? ?(2).任何仅在pivot for 子句中引用的列,不能用在select 列表中; ? ?(3).pivot 子句中的全部列都必需行使聚积函数。 ? ? ?1.1 示例:单个字段聚积 create table pivot_table as
select *
from (select t.job,t.deptno,t.sal from emp t)
pivot(sum(sal) --pivot_clause 界说要举办聚积的列
for deptno --pivot_for_clause 界说要分组和转置的列
in(10 dept_10,20 dept_20,30 dept_30))
--pivot_in_clause 界说限制功效集的值的范畴。发生的每个值的聚积转换为单唯一列。
? ?1.2 示例:多个字段聚积 --pivot 行使多个聚积
select *
from (select t.job,t.sal from emp t)
pivot(sum(sal) sal,count(sal) cnt
for deptno
in(10 dept_10,30 dept_30))
where job in (‘MANAGER‘,‘CLERK‘);
? 2.unpivot 列转行 ? ?unpivot 用法: select ...
from ...
pivot [include nulls|exclude nulls]
(unpivot_clause
unpivot_for_clause
unpivot_in_clause)
where ...
(1)unpivot_clause: 界说暗示反转置值后的列名称(列名对应的列值);
(2)unpivot_for_clause: 界说反转置查询所获得列的列名称(列名);
(3)unpivot_in_clause: 界说要举办反转置的已转置列(不是值)的列表。
? ? ?2.1 示例:列转行 ,在unpivot_in_clause 中可以行使 as 行使别名 select *
from (select * from pivot_table )
unpivot (sal_value
for dept_column
in (dept_10 as ‘10‘,dept_20,dept_30)); --行使别名
? ? ?2.2 示例:(1) include nulls ; (2) 转置数据范例需同等 --unpivot 列传行
/* 行使unpivot函数,全部转置列数据范例必要同等,这是UNPIVOT查询执行上的一个限定,
不然会报错:ora-01790 */
select *
from (select to_char(empno) empno,t.ename,t.job,to_char(t.mgr) mgr,to_char(t.hiredate,‘yyyy-mm-dd‘) hiredate,to_char(t.sal) sal,to_char(t.comm) comm,to_char(t.deptno) deptno
from emp t
where rownum = 1)
unpivot include nulls(colnum_value
for colnum_name
in(empno,ename,job,mgr,hiredate,sal,comm,deptno)) ;
(编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |





