CubeRollup Slides PDF
CubeRollup Slides PDF
CubeRollup Slides PDF
John King
King Training Resources
6341 South Williams Street
Littleton, CO 80121-2627 USA
www.kingtraining.com
800.252.0652 or 303.798.5727
Analytic clause
Query_partition_clause-Order_by clause-Windowing clause
Query partition clause
PARTITION BY list,of,cols
Windowing clause
RANGE or ROWS ...
Order by clause
ORDER BY col,list
Analytic Function: RANK (8.1.6)
RANK provides rankings of values with gaps where sets
of rows have equal values
(DENSE_RANK removes gaps)
SQL> run
1 select deptno,ename,sal,
RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) salrank
2 from emp where deptno in (10,30)
DEPTNO ENAME SAL SALRANK
----------- ----------------- ---------- --------------
10 KING 5000 1
10 CLARK 2450 2
10 MILLER 1300 3
30 BLAKE 2850 1
30 ALLEN 1600 2
30 TURNER 1500 3
30 MARTIN 1250 4
30 WARD 1250 4
30 JAMES 950 6
Old Aggregate, New Usage (8.1.6)
Analytic function clauses may be used with many existing
aggregates
SQL> run
1 select deptno, ename, sal,
2 ,round(avg(sal) OVER (PARTITION BY deptno) , 0) avg_sal
3 from emp
4* where deptno in (10,20)
DEPTNO ENAME SAL AVG_SAL
----------- ---------------- ---------- ----------
10 KING 5000 2917
10 CLARK 2450 2917
10 MILLER 1300 2917
20 JONES 2975 2175
20 FORD 3000 2175
20 SMITH 800 2175
20 SCOTT 3000 2175
20 ADAMS 1100 2175
ROW_NUMBER (8.1.6)
ROW_NUMBER allows ranking by criteria
SQL> run
1 select deptno, ename, sal,
2 ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY by sal desc)
3 sal_rank
4 from emp
5* where deptno in (10,20)
DEPTNO ENAME SAL SAL_RANK
----------- ----------------- ---------- ---------------
10 KING 5000 1
10 CLARK 2450 2
10 MILLER 1300 3
20 FORD 3000 1
20 SCOTT 3000 2
20 JONES 2975 3
20 ADAMS 1100 4
20 SMITH 800 5
Cube and Rollup
K CUBE and ROLLUP extend GROUP BY
K ROLLUP builds subtotal aggregates at any level,
including grand total
K CUBE extends ROLLUP to calculate all possible
combinations of subtotals for a GROUP BY
K Cross-tabulation reports are easy with CUBE
K Oracle8i Release 2 (Oracle version 8.1.6) began release
in February 2000, its new Analytic functions include:
ranking, moving aggregates, period comparisons,
ratio of total, and cumulative aggregates
Normal GROUP BY Functionality
Normally, GROUP BY allows aggregates
(sub-totals) by specific column or set of columns
Before Oracle8i SQL required JOIN or UNION to combine
subtotal information and grand totals in a single SQL
query
ROLLUP creates subtotals and grand totals in the same
query along with intermediate subtotals
CUBE adds cross-tabulation information based upon the
GROUP BY columns
GROUP BY (without CUBE or ROLLUP)
begin
dbms_mview.refresh('dept summary tab');
end;
/
Conclusion
CUBE and ROLLUP reduce work necessary to code and
create aggregates often requested by management to
provide competitive or summary information
CUBE and ROLLUP provide mechanisms for using a
single SQL statement to provide data that would have
required multiple SQL statements, programming, or
manual summarization in the past
Materialized Views reduce the impact of frequently
executed queries by storing results and refreshing them
on a periodic basis
These tools may be used to mine Oracle databases for
the golden information frequently in demand today
To contact the author:
John King
King Training Resources
6341 South Williams Street
Littleton, CO 80121-2627 USA
1.800.252.0652 - 1.303.798.5727
Email: john@kingtraining.com