Connecting Tech Pros Worldwide Forums | Help | Site Map

Pivoting - 2

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,569
#1   Sep 17 '07
Displaying Histogram-Horizontal
===========================
Expand|Select|Wrap|Line Numbers
  1. select deptno,lpad('*',count(*),'*') as cnt from emp group by deptno
Histogram-Vertical
===============
Expand|Select|Wrap|Line Numbers
  1. select row_number( )over(partition by deptno order by empno) rn,
  2. case when deptno=10 then '*' else null end deptno_10,
  3. case when deptno=20 then '*' else null end deptno_20,
  4. case when deptno=30 then '*' else null end deptno_30
  5. from emp
Displaying only the histogram
============================
Expand|Select|Wrap|Line Numbers
  1. select max(deptno_10) d10,
  2.        max(deptno_20) d20,
  3.        max(deptno_30) d30
  4.   from (
  5. select row_number( )over(partition by deptno order by empno) rn,
  6.        case when deptno=10 then '*' else null end deptno_10,
  7.        case when deptno=20 then '*' else null end deptno_20,
  8.        case when deptno=30 then '*' else null end deptno_30
  9.   from emp
  10.        ) x
  11.  group by rn
  12.  order by 1 desc, 2 desc, 3 desc
Returning Non-GROUP BY Columns
============================
Expand|Select|Wrap|Line Numbers
  1. select deptno,ename,job,sal,
  2.        case when sal = max_by_dept
  3.             then 'TOP SAL IN DEPT'
  4.             when sal = min_by_dept
  5.             then 'LOW SAL IN DEPT'
  6.        end dept_status,
  7.        case when sal = max_by_job
  8.             then 'TOP SAL IN JOB'
  9.             when sal = min_by_job
  10.             then 'LOW SAL IN JOB'
  11.        end job_status
  12.   from (
  13. select deptno,ename,job,sal,
  14.        max(sal)over(partition by deptno) max_by_dept,
  15.        max(sal)over(partition by job)   max_by_job,
  16.        min(sal)over(partition by deptno) min_by_dept,
  17.        min(sal)over(partition by job)   min_by_job
  18.   from emp
  19.        ) emp_sals
  20.  where sal in (max_by_dept,max_by_job,
  21.                min_by_dept,min_by_job)
Using ROLLUP to display total
=============================
Expand|Select|Wrap|Line Numbers
  1. select case grouping(job)
  2.             when 0 then job
  3.             else 'TOTAL'
  4.        end job,
  5.        sum(sal) sal
  6.   from emp
  7.  group by rollup(job)
ROLLUP (10g):- TO GENERATE TOTALS AND SUB-TOTALS IN THE SUMMERISED RESULT.IT CAN ONLY APPEAR IN A QUERY WITH A GROUP BY CLAUSE.

Expand|Select|Wrap|Line Numbers
  1. select empno,sum(sal) from emp group by rollup(empno);
  2.  
  3. select empno,ename,sum(sal) from emp group by rollup(empno,ename);
  4.  
  5. select empno,ename,sum(sal) from emp group by rollup(ename,empno);
  6.  
  7. select empno,ename,sum(sal),avg(sal) from emp group by rollup(ename,empno);
  8.  
  9. select grouping(empno),empno,ename,sum(sal),avg(sal) from emp group by rollup(ename,empno);


Also check Pivoting - 3



Reply


Similar Oracle Database bytes