|
|
|
Using Oracle 8i's ROLLUP Feature
ROLLUP
ROLLUP enables a SELECT statement to calculate multiple levels of
subtotals across a specified group of dimensions. It also calculates a grand total. ROLLUP is a simple extension to the GROUP BY clause, so its syntax is extremely easy to use. The ROLLUP extension is highly efficient, adding minimal overhead to a query.Syntax
ROLLUP appears in the GROUP BY clause in a SELECT statement. Its form
is:SELECT ... GROUP BY
Details
ROLLUP's action is straightforward: it creates subtotals which "roll up"
from the most detailed level to a grand total, following a grouping list specified in the ROLLUP clause. ROLLUP takes as its argument an ordered list of grouping columns. First, it calculates the standard aggregate values specified in the GROUP BY clause. Then, it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. Finally, it creates a grand total.ROLLUP will create subtotals at n+1 levels, where n is the number of
grouping columns. For instance, if a query specifies ROLLUP on grouping columns of Time, Region, and Department ( n=3), the result set will include rows at four aggregation levels.Example
This example of ROLLUP uses the data in the video store database.
sum(Profit) AS Profit FROM salesSELECT Time, Region, Department,
As you can see in the following table, this query returns the following sets of rows:
without using ROLLUP
- Regular aggregation rows that would be produced by GROUP BY
ROLLUP Aggregation across Three Dimensions
|
Time |
Region |
Department |
Profit |
|
1996 |
Central |
VideoRental |
75,000 |
|
1996 |
Central |
VideoSales |
74,000 |
|
1996 |
Central |
[NULL] |
149,000 |
|
1996 |
East |
VideoRental |
89,000 |
|
1996 |
East |
VideoSales |
115,000 |
|
1996 |
East |
[NULL] |
204,000 |
|
1996 |
West |
VideoRental |
87,000 |
|
1996 |
West |
VideoSales |
86,000 |
|
1996 |
West |
NULL] |
173,000 |
|
1996 |
[NULL] |
[NULL] |
526,000 |
|
1997 |
Central |
VideoRental |
82,000 |
|
1997 |
Central |
VideoSales |
85,000 |
|
1997 |
Central |
[NULL] |
167,000 |
|
1997 |
East |
VideoRental |
101,000 |
|
1997 |
East |
VideoSales |
137,000 |
|
1997 |
East |
[NULL] |
238,000 |
|
1997 |
West |
VideoRental |
96,000 |
|
1997 |
West |
VideoSales |
97,000 |
|
1997 |
West |
[NULL] |
193,000 |
|
1997 |
[NULL] |
[NULL] |
598,000 |
|
[NULL] |
[NULL] |
[NULL] |
1,124,000 |
Calculating Subtotals without ROLLUP
The result set in the table could be generated by the UNION of four
SELECT statements, as shown below. This is a subtotal across three dimensions. Notice that a complete set of ROLLUP-style subtotals in n dimensions would require n+1 SELECT statements linked with UNION ALL.SELECT Time, Region, Department, SUM(Profit)
FROM Sales
GROUP BY Time, Region, Department
UNION ALL
SELECT Time, Region, '' , SUM(Profit)
FROM Sales
GROUP BY Time, Region
UNION ALL
SELECT Time, '', '', SUM(Profits)
FROM Sales
GROUP BY Time
UNION ALL
SELECT '', '', '', SUM(Profits)
FROM Sales;
The approach shown in the SQL above has two shortcomings compared to
using the ROLLUP operator. First, the syntax is complex, requiring more effort to generate and understand. Second, and more importantly, query execution is inefficient because the optimizer receives no guidance about the user's overall goal. Each of the four SELECT statements above causes table access even though all the needed subtotals could be gathered with a single pass. The ROLLUP extension makes the desired result explicit and gathers its results with just one table access.The more columns used in a ROLLUP clause, the greater the savings versus
the UNION approach. For instance, if a four-column ROLLUP replaces a UNION of 5 SELECT statements, the reduction in table access is four-fifths or 80%.Some data access tools calculate subtotals on the client side and
thereby avoid the multiple SELECT statements described above. While this approach can work, it places significant loads on the computing environment. For large reports, the client must have substantial memory and processing power to handle the subtotaling tasks. Even if the client has the necessary resources, a heavy processing burden for subtotal calculations may slow down the client in its performance of other activities.When to Use ROLLUP
Use the ROLLUP extension in tasks involving subtotals.
Vijays
Datacons
'Shanthishree' , 17/1, Hosur Road, Bangalore - 560 068, India
Ph : 91-080-5723233/5724919,5725255/6/7
vijays@dcons.com
visit us at: http://www.dcons.com