0% found this document useful (0 votes)
85 views26 pages

Week-10b Rollup Cube Grouping

Here are the queries for the tasks: 1. SELECT mgr, job, SUM(sal) AS total_salary FROM emp WHERE mgr < 7782 GROUP BY ROLLUP(mgr, job); 2. SELECT mgr, job, SUM(sal) AS total_salary, GROUPING(mgr) AS mgr_grouping, GROUPING(job) AS job_grouping FROM emp WHERE mgr < 7782 GROUP BY ROLLUP(mgr, job); 3. SELECT mgr, job, SUM(sal) AS total_salary, FROM emp WHERE mgr < 7782 GROUP BY CUBE(mgr, job); 4. SELECT mgr, job,

Uploaded by

Kenan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views26 pages

Week-10b Rollup Cube Grouping

Here are the queries for the tasks: 1. SELECT mgr, job, SUM(sal) AS total_salary FROM emp WHERE mgr < 7782 GROUP BY ROLLUP(mgr, job); 2. SELECT mgr, job, SUM(sal) AS total_salary, GROUPING(mgr) AS mgr_grouping, GROUPING(job) AS job_grouping FROM emp WHERE mgr < 7782 GROUP BY ROLLUP(mgr, job); 3. SELECT mgr, job, SUM(sal) AS total_salary, FROM emp WHERE mgr < 7782 GROUP BY CUBE(mgr, job); 4. SELECT mgr, job,

Uploaded by

Kenan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

ENHANCED GROUP BY OPERATORS

ROLLUP
CUBE
GROUPING SETS
GROUPING
GROUPING_ID
Lesson Aim

In this lesson you learn how to:

• Group data for obtaining the following:


– Subtotal values by using the ROLLUP operator
– Cross-tabulation values by using the CUBE operator

• Use the GROUPING function to identify the level of aggregation in the


results set produced by a ROLLUP or CUBE operator.

• Use GROUPING SETS to produce a single result set that is equivalent to


a UNION ALL approach
The ROLLUP Operator

The ROLLUP operator delivers aggregates and superaggregates for


expressions within a GROUP BY statement. The ROLLUP operator can be
used by report writers to extract statistics and summary information from
results sets. The cumulative aggregates can be used in reports, charts, and
graphs.

The ROLLUP operator creates groupings by moving in one direction, from right
to left, along the list of columns specified in the GROUP BY clause. It then
applies the aggregate function to these groupings.
The CUBE Operator

The CUBE operator is an additional switch in the GROUP BY clause in a


SELECT statement. The CUBE operator can be applied to all aggregate
functions, including AVG, SUM, MAX, MIN, and COUNT. It is used to produce
results sets that are typically used for cross-tabular reports. While ROLLUP
produces only a fraction of possible subtotal combinations, CUBE produces
subtotals for all possible combinations of groupings specified in the GROUP BY
clause, and a grand total.

The CUBE operator is used with an aggregate function to generate additional


rows in a results set. Columns included in the GROUP BY clause are cross-
referenced to produce a superset of groups. The aggregate function specified
in the select list is applied to these groups to produce summary values for the
additional superaggregate rows. The number of extra groups in the results set
is determined by the number of columns included in the GROUP BY clause.

In fact, every possible combination of the columns or expressions in the


GROUP BY clause is used to produce superaggregates. If you have n columns
or expressions in the GROUP BY clause, there will be 2n possible
superaggregate combinations. Mathematically, these combinations form an
n-dimensional cube, which is how the operator got its name.
The GROUPING Function

The GROUPING function can be used with either the CUBE or ROLLUP operator to help
you understand how a summary value has been obtained.

The GROUPING function uses a single column as its argument. The expr in the
GROUPING function must match one of the expressions in the GROUP BY clause. The
function returns a value of 0 or 1.

The values returned by the GROUPING function are useful to:


• Determine the level of aggregation of a given subtotal; that is, the group or groups on
which the subtotal is based
• Identify whether a NULL value in the expression column of a row of the result set
indicates:
– A NULL value from the base table (stored NULL value)
– A NULL value created by ROLLUP/CUBE (as a result of a group function on that
expression)

A value of 0 returned by the GROUPING function based on an expression indicates one of


the following:
• The expression has been used to calculate the aggregate value.
• The NULL value in the expression column is a stored NULL value.

A value of 1 returned by the GROUPING function based on an expression indicates one of


the following:
• The expression has not been used to calculate the aggregate value.
• The NULL value in the expression column is created by ROLLUP or CUBE as a result
of grouping.
GROUPING SETS

GROUPING SETS are a further extension of the GROUP BY clause that let you specify

multiple groupings of data. Doing so facilitates efficient aggregation and hence facilitates

analysis of data across multiple dimensions.

A single SELECT statement can now be written using GROUPING SETS to specify various

groupings (that can also include ROLLUP or CUBE operators), rather than multiple

SELECT statements combined by UNION ALL operators


Summary

• ROLLUP and CUBE are extensions of the GROUP BY clause.

• ROLLUP is used to display subtotal and grand total values.

• CUBE is used to display cross-tabulation values.

• The GROUPING function helps you determine whether a row is an aggregate produced by
a CUBE or ROLLUP operator.

• With the GROUPING SETS syntax, you can define multiple groupings in the same query.
GROUP BY computes all the groupings specified and combines them with UNION ALL.

• Within the GROUP BY clause, you can combine expressions in various ways:

– To specify composite columns, you group columns within parentheses so that the
Oracle Server treats them as a unit while computing ROLLUP or CUBE operations.

– To specify concatenated grouping sets, you separate multiple grouping sets,


ROLLUP, and CUBE operations with commas so that the Oracle Server combines them
into a single GROUP BY clause. The result is a cross-product of groupings from each
grouping set.
SAMPLE-1 : ROLLUP
SELECT job ,SUM(sal) AS "TOTAL SALARY“ FROM emp GROUP BY ROLLUP(job) ;

SELECT job,SUM(sal) SELECT NULL,SUM(sal)


FROM emp GROUP BY job FROM emp

SELECT job,SUM(sal) FROM emp


GROUP BY job
UNION ALL
SELECT NULL,SUM(sal)
FROM emp
SELECT * FROM GRADES ORDER BY ID ;
SAMPLE-2 : ROLLUP

SELECT semester,AVG(score)
FROM GRADES
GROUP BY ROLLUP(semester)

SELECT semester,AVG(score) FROM GRADES GROUP BY semester

UNION ALL

SELECT null, AVG(score) FROM GRADES;


SAMPLE-3 : ROLLUP
SELECT class, semester, AVG(score)
FROM GRADES
GROUP BY ROLLUP(class,semester) ;

SELECT class,semester, AVG(score) FROM GRADES GROUP BY class,semester


UNION ALL
SELECT class,NULL,AVG(score) FROM GRADES GROUP BY class
UNION ALL
SELECT NULL,NULL,AVG(score) FROM GRADES ;
SAMPLE-4 : CUBE
SELECT class,semester, AVG(score) FROM GRADES
GROUP BY CUBE(class,semester)

SELECT NULL,NULL,AVG(score) FROM GRADES


UNION ALL
SELECT NULL,semester,AVG(score) FROM GRADES GROUP BY semester
UNION ALL
SELECT class,NULL,AVG(score) FROM GRADES GROUP BY class
UNION ALL
SELECT class, semester,AVG(score) FROM GRADES GROUP BY class,semester;

SELECT class,semester, AVG(score) FROM GRADES


GROUP BY GROUPING SETS ( (class,semester), class,semester, ( ) )
SAMPLE-5 : GROUPING SETS

SELECT class,semester,AVG(SCORE) FROM GRADES

GROUP BY GROUPING SETS (class,semester)

SELECT CLASS,NULL AS SEMESTER, AVG(SCORE) FROM GRADES GROUP BY CLASS


UNION ALL
SELECT NULL,SEMESTER,AVG(SCORE) FROM GRADES GROUP BY SEMESTER
SAMPLE-6 : GROUPING SETS

SELECT class,semester, AVG(SCORE) FROM GRADES

GROUP BY GROUPING SETS ( (class,semester) ) ;

SELECT class,semester, AVG(score)

FROM GRADES

GROUP BY class,semester ;
SAMPLE-7 : GROUPING SETS

SELECT class,semester, AVG( score ) FROM GRADES

GROUP BY GROUPING SETS ( (class,semester), class,semester, ( ) ) ;


SAMPLE-8 : GROUPING SETS

SELECT class,semester, AVG(score) FROM GRADES

GROUP BY GROUPING SETS( (class,semester),class )

SELECT class,semester, AVG(score ) FROM GRADES GROUP BY class,semester

UNION ALL

SELECT score , NULL, AVG( score ) FROM GRADES GROUP BY class ;


SAMPLE-9 : GROUPING SETS and GROUPING_ID

SELECT class, semester, AVG(score) FROM GRADES


GROUP BY GROUPING SETS ( (class,semester), semester ) ;

SELECT class, semester, AVG( score ) FROM GRADES GROUP BY class, semester
UNION ALL
SELECT semester, NULL, AVG(score) FROM GRADES GROUP BY semester ;

SELECT class, semester, AVG(score) FROM GRADES GROUP BY GROUPING SETS ( (class, semester),semester )

ORDER BY GROUPING_ID(class);

SELECT class, semester, AVG(score) FROM GRADES GROUP BY GROUPING SETS ( (class, semester),semester )

ORDER BY GROUPING_ID(class),class,semester;
SAMPLE-10 : GROUPING SETS and GROUPING_ID

SELECT class,semester, SUM(score),


GROUPING(class), GROUPING (semester),
GROUPING _ID(class),
GROUPING _ID(semester,class),
GROUPING _ID(semester),
FROM GRADES GROUP BY
GROUPING SETS ( (class),(semester,class),(semester), ( ) ) ;
TASKS
USE emp TABLE for all tasks ;
1- Write a query to display the following for those employees whose MGR is less than 7782;
-MGR
-JOB and total salary for every job for employees who report to the same manager
-Total salary of those managers
-Total salary of those managers, irrespective (безотносительный) of the jobs
2- Observe the output from task 1. Write a query using the GROUPING function to
determine whether the NULL values in the columns corresponding to the GROUP BY
expressions are caused by the ROLLUP operation.
3- Write a query to display the following for those employees whose MGR is less than 7782;
-MGR
-Job and total salaries for every job for employees who report to the same manager
-Total salary of those managers
-Cross-tabulation values to display the total salary for every job, irrecpective of the manager
-Total salary irrecpective of all jobs
4- Observe the output from task-3. Write a query using the GROUPING function to determine
whether the NULL values in the columns corresponding to the GROUP BY expressions are
caused by the CUBE operation
5- Using GROUPING SETS, write a query to display the followings :
-deptno , mgr , job
-deptno, job
-mgr , job
The query should calculate the sum of the salaries for
each of these groups.
6- Use EMP table
Write a query to display the following for those employees;
-Total salary of employees from every job on every departments
-Total salary of all employees on every departments
-Total salary of all jobs on all departments

You might also like