Oracle row-to-column and column-changing careers

Source: Internet
Author: User

1  withL as2(SELECT  Level  asLv fromDUAL CONNECT by  Level <= 9  ),3M as (4 SELECTa.lv asLv_a,5b.lv asLv_b,6To_char (b.lv)|| 'x' ||To_char (a.lv)|| '=' ||7Rpad (To_char (a.lv*B.LV),2,' ') as TEXT8    froml A, l B9  WHEREb.lv<=a.lv)Ten  SELECTListagg (M.text,' ') withinGROUP(ORDER  byM.lv_b) as99 Table One   fromm A  GROUP  bym.lv_a -;

Set up practice exercises;

1 SELECTDepartment, NVL (staff,0.00), NVL (salesman,0.00), NVL (Chairman,0.00), NVL (general manager,0.00), NVL (analyst,0.00)2  from (3 SELECT4To_char (A.deptno) asDepartment,5       MIN(DECODE (A.job,'Clerk', A.avg_sal)) Staff,6       MIN(DECODE (A.job,'salesman', A.avg_sal)) Salesman,7       MIN(DECODE (A.job,'President', A.avg_sal)) Chairman,8       MIN(DECODE (A.job,'MANAGER', A.avg_sal)) General Manager,9       MIN(DECODE (A.job,'ANALYST', A.avg_sal)) AnalystTen  from ( One SELECT A E.deptno, - E.job, -       ROUND(AVG(SAL),5) Avg_sal the  fromEMP E - GROUP  byE.job,e.deptno - ORDER  byE.deptno - ) A + GROUP  byA.deptno - ORDER  byA.deptno + ) A UNION  All  at SELECTDepartment, NVL (staff,0.00), NVL (salesman,0.00), NVL (Chairman,0.00), NVL (general manager,0.00), NVL (analyst,0.00) -   from(  - SELECT  -         'position sum'  asDepartment, -         Max(sum(DECODE (Job,'Clerk', Sal))) asStaff, -         Max(sum(DECODE (Job,'salesman', Sal))) assalesman, in         Max(sum(DECODE (Job,'President', Sal))) aschairman, -         Max(sum(DECODE (Job,'MANAGER', Sal))) asGeneral Manager, to        Max(sum(DECODE (Job,'ANALYST', Sal))) asAnalyst +  fromEMP - GROUP  byJob the  * )  $ UNION  AllPanax Notoginseng SELECTDepartment, NVL (staff,0.00), NVL (salesman,0.00), NVL (Chairman,0.00), NVL (general manager,0.00), NVL (analyst,0.00) -   from(  the SELECT  +         'Average'  asDepartment, A         min(DECODE (Job,'Clerk',ROUND(AVG(SAL),5))) asStaff, the         min(DECODE (Job,'salesman',ROUND(AVG(SAL),5))) assalesman, +         min(DECODE (Job,'President',ROUND(AVG(SAL),5))) aschairman, -         min(DECODE (Job,'MANAGER',ROUND(AVG(SAL),5))) asGeneral Manager, $         min(DECODE (Job,'ANALYST',ROUND(AVG(SAL),5))) asAnalyst $  fromEMP - GROUP  byJob -  the);

A simplified version of the example above:

1 /*2 1. First use decode () to perform row-to-column detection of basic data as a table!;;3 The 2.B table summarizes the data in table A and merges it;4 */ 5  withA as ( 6 SELECT 7To_char (DEPTNO) asDeptno,8       SUM(DECODE (Job,'ANALYST', Sal,0)) asANALYST,9       SUM(DECODE (Job,'Clerk', Sal,0)) asClerk,Ten       SUM(DECODE (Job,'MANAGER', Sal,0)) asMANAGER, One       SUM(DECODE (Job,'President', Sal,0)) asPresident, A       SUM(DECODE (Job,'salesman', Sal,0)) assalesman, -       SUM(SAL) assum -  fromScott. EMP the GROUP  byDeptno - ORDER  by 1 ), -B as ( - SELECT 'sum',  +        sum(ANALYST), -        sum(Clerk), +        sum(MANAGER), A        sum(President), at        sum(salesman), -        SUMSum froma) - SELECT *  froma - UNION  All  - SELECT *  fromb;

Oracle row-to-column and column-changing careers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.