Week-10b Rollup Cube Grouping
Week-10b Rollup Cube Grouping
ROLLUP
CUBE
GROUPING SETS
GROUPING
GROUPING_ID
Lesson Aim
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 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.
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
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
• 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.
SELECT semester,AVG(score)
FROM GRADES
GROUP BY ROLLUP(semester)
UNION ALL
FROM GRADES
GROUP BY class,semester ;
SAMPLE-7 : GROUPING SETS
UNION ALL
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