Connecting Tech Pros Worldwide Forums | Help | Site Map

Pivoting - 3

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,508
#1   Sep 17 '07
CUBE:-IT GENERATES SUBTOTAL FOR ALL POSSIBLE COMBINATION OF GROUPED COLUMNS.

GROUPING SETS:-GENERATES SUMMARY INFORMATION AT THE CHOOSEN LEVEL,WITHOUT INCLUDING ALL THE ROWS PRODUCED BY REGULAR GROUP BY OPERATION.

GROUPING,GROUPING_ID,GROUP_ID :-HELPS TO CORRECTLY INTERPRET RESULTS GENERATED USING ROLLUP,CUBE AND GROUPING SETS.

Expand|Select|Wrap|Line Numbers
  1. select empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno);
  2.  
  3. select decode(grouping(empno),1,'all empno',empno) as no,empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno);
  4.  
  5.  
  6. select decode(grouping(empno),1,'all empno',empno) as no,empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno)
  7.  
  8. select empno,ename,avg(sal) from emp group by grouping sets(empno,ename)
The value from GROUPING(JOB) will be 1 or 0 depending on whether or not the values for SAL are due to the GROUP BY or the CUBE. If the results are due to the CUBE, the value will be 1, otherwise it will be 0.
----------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select deptno,
  2.        job,
  3.        case grouping(deptno)||grouping(job)
  4.             when '00' then 'TOTAL BY DEPT AND JOB'
  5.             when '10' then 'TOTAL BY JOB'
  6.             when '01' then 'TOTAL BY DEPT'
  7.             when '11' then 'GRAND TOTAL FOR TABLE'
  8.        end category,
  9.        sum(sal) sal
  10.   from emp
  11.  group by cube(deptno,job)
  12.  order by grouping(job),grouping(deptno)
Few more sample query
==================
Sample #1
===========
Expand|Select|Wrap|Line Numbers
  1. select deptno, job, sum(sal) sal,
  2.        grouping(deptno) deptno_subtotals,
  3.        grouping(job) job_subtotals
  4.   from emp
  5.  group by cube(deptno,job)
Sample #2
===========
Expand|Select|Wrap|Line Numbers
  1. select ename,
  2.         job,
  3.         case when job = 'CLERK'
  4.              then 1 else 0
  5.         end as is_clerk,
  6.         case when job = 'SALESMAN'
  7.              then 1 else 0
  8.         end as is_sales,
  9.         case when job = 'MANAGER'
  10.              then 1 else 0
  11.         end as is_mgr,
  12.         case when job = 'ANALYST'
  13.             then 1 else 0
  14.         end as is_analyst,
  15.         case when job = 'PRESIDENT'
  16.             then 1 else 0
  17.         end as is_prez
  18.    from emp
  19.   order by 2
Sample #3
===========
Expand|Select|Wrap|Line Numbers
  1. select max(case deptno when 10 then ename end) d10,
  2.         max(case deptno when 20 then ename end) d20,
  3.         max(case deptno when 30 then ename end) d30,
  4.         max(case job when 'CLERK' then ename end) clerks,
  5.         max(case job when 'MANAGER' then ename end) mgrs,
  6.         max(case job when 'PRESIDENT' then ename end) prez,
  7.         max(case job when 'ANALYST' then ename end) anals,
  8.         max(case job when 'SALESMAN' then ename end) sales
  9.    from (
  10.  select deptno, job, ename,
  11.         row_number()over(partition by deptno order by empno) rn
  12.    from emp
  13.         ) x
  14.   group by rn
Performing Aggregations over Different Groups/Partitions Simultaneously
================================================== ====
Expand|Select|Wrap|Line Numbers
  1. select ename,deptno, count(*)over(partition by deptno) deptno_cnt, job,
  2. count(*)over(partition by job) job_cnt,
  3. count(*)over() total from emp
Performing Aggregations over a Moving Range of Values
==========================================
Expand|Select|Wrap|Line Numbers
  1. select hiredate,
  2.        sal,
  3.        sum(sal)over(order by hiredate
  4.                        range between 90 preceding
  5.                          and current row) spending_pattern
  6.   from emp e
Pivoting a Result Set with Subtotals
============================
Expand|Select|Wrap|Line Numbers
  1. select mgr,
  2.        sum(case deptno when 10 then sal else 0 end) dept10,
  3.        sum(case deptno when 20 then sal else 0 end) dept20,
  4.        sum(case deptno when 30 then sal else 0 end) dept30,
  5.        sum(case flag when '11' then sal else null end) total
  6.   from (
  7. select deptno,mgr,sum(sal) sal,
  8.        cast(grouping(deptno) as char(1))||
  9.        cast(grouping(mgr) as char(1)) flag
  10.   from emp
  11.  where mgr is not null
  12.  group by rollup(deptno,mgr)
  13.        ) x
  14.  group by mgr



Reply


Similar Oracle Database bytes