oracle pivot / unpivot

1.pivot line transfer column

   pivot Usage:

SELECT ...
 from ... 
Pivot - > Note: between keywords and from where the 
      (pivot_clause 
       pivot_for_clause 
       pivot_in_clause) 
where ... 
( . 1 ) pivot_clause: aggregating define columns; 
( 2 ) pivot_for_clause: defined to transpose packets and columns; 
( . 3 ) pivot_in_clause: defining a range of values defined result. Generating aggregation convert each individual value is a.

   pivot Note:

   . (1) only referenced in clause pivot column, it can not be used in the select list;

   (2) any reference in the pivot for only clause columns can not be used in the select list;

   (3) all columns .pivot clause must use aggregate functions.

 

   Example 1.1: a single field aggregation

Create  Table the pivot_table AS 
SELECT  * 
  from ( SELECT t.job, t.deptno, t.sal from EMP T) 
Pivot ( SUM (SAL) - pivot_clause column defined to be aggregated 
   for DEPTNO   - pivot_for_clause packet and define the transpose column 
     in ( 10 dept_10, 20 is dept_20, 30 dept_30))
                - pivot_in_clause limit the scope of the definition of the value of the result set. Generating aggregation convert each individual value is a.

   Example 1.2: gathering a plurality of fields

--pivot 使用多个聚集
select *
  from (select t.job, t.deptno, t.sal from emp t)
pivot(sum(sal) sal,count(sal) cnt
   for deptno  
     in(10 dept_10, 20 dept_20, 30 dept_30))
  where job in ('MANAGER','CLERK');

 

2.unpivot column switch

   unpivot usage:

SELECT ...
   from ... 
  Pivot [ the include NULLS | NULLS the exclude ] 
        (unpivot_clause 
         unpivot_for_clause 
         unpivot_in_clause) 
  WHERE ... 
  ( . 1 ) unpivot_clause: definition represents column name (column name corresponding to the column values) set value after inversion; 
  ( 2 ) unpivot_for_clause: define home reverse query column name (column name) column is obtained; 
  ( 3 ) unpivot_in_clause: to reverse the list of definitions set has been transposed column (not the value) of.

 

   Example 2.1: column switch, may be used as the alias in unpivot_in_clause

select * 
  from (select * from pivot_table )
  unpivot (sal_value  
    for dept_column
      in (dept_10 as '10' ,dept_20,dept_30)); --使用别名

 

   Example 2.2: (1) include nulls; (2) must be the same data type transpose

- unpivot Biography row 
/ * use unpivot function, transpose all the column data type needs to be consistent, which is a restriction on UNPIVOT query execution, 
   otherwise it will error: ORA-01.79 thousand * / 
   
the SELECT  * 
  from ( the SELECT the 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 the include NULLS (colnum_value
      for colnum_name
        in(empno,ename,job,mgr,hiredate,sal,comm,deptno)) ;

 

Guess you like

Origin www.cnblogs.com/jason3361/p/11124579.html