You are on page 1of 2

create table xtern_empl_rpt

( empl_id varchar2(3),
last_name varchar2(50),
first_name varchar2(50),
ssn varchar2(9),
email_addr varchar2(100),
years_of_service number(2,0)
)
organization external
( default directory xtern_data_dir
access parameters
( records delimited by newline
fields terminated by ','
)
location ('employee_report.csv')
);
===================================================
ROLLUP and CUBE Operators in Oracle
Sometimes, a simple GROUP BY clause just isn t enough in a query. Once you generat
e a report of, let s say, average salary by department or the standard deviation o
f sick days by job title, you often must run a second query that calculates the
average salary or standard deviation across the entire set of employees. It gets
even more complex when you break down the average salary by more than one facto
r, such as department and job title. In this case, you would need to run two or
more additional queries to produce the average salary just by department or for
the entire workforce.
Tip : The results from both CUBE and ROLLUP can be produced by multiple queries,
but this requires multiple passes over the rows in the table. CUBE and ROLLUP n
eed only one pass
The ROLLUP operator provides rollups of aggregate functions in one direction acr
oss the fields that are aggregated. For each ROLLUP operation that uses n column
s, the result set has aggregates for each combination of columns and n+1 groupin
gs.
The CUBE operator takes the ROLLUP operator a step further and provides rollups
of aggregate functions in both directions across the fields that are to be aggre
gated. For each CUBE operation that uses n columns, the result set has aggregate
s for each combination of columns plus 2n groupings.
ROLLUP
The boss asks Janice to give him a report that breaks down the average salary by
both department and job function for departments 10 through 90. Janice wants to
save time writing the query, and she knows by now that King will want to see so
me subtotals and grand totals. She will use ROLLUP to accomplish the task in a s
ingle query, as follows:
select department_id "Dept", job_id "Job",
avg(salary) "Avg Sal"
from employees
where department_id between 10 and 90
group by rollup(department_id, job_id);

Notice that because Janice has two columns listed in her ROLLUP clause, she wil
l have three (two plus one) types of groupings in the query output:

* Combinations of departments and jobs (for example, 30 and PU_CLERK, with a


n average salary of 2780)
* Summaries by departments (for example, 20 and a NULL job title, with an av
erage salary of 9500)
* A grand total (NULL department number and NULL job title, with an average
salary for all employees in all departments of 6250)

CUBE

The report that Janice wrote for King using the ROLLUP operator was fine until he
wanted to know summaries by job title also. Janice realizes that she should have
given him the version of the query using CUBE to begin with, so she changes her
previous query, substituting the keyword CUBE for ROLLUP:
select department_id "Dept", job_id "Job",
avg(salary) "Avg Sal"
from employees
where department_id between 10 and 90
group by cube(department_id, job_id);

Using CUBE, she has two columns listed in our ROLLUP clause and therefore will h
ave four (two squared) types of groupings in the query output:
* Combinations of departments and jobs (for example, 30 and PU_CLERK, with a
n average salary of 2780)
* Summaries by jobs (for example, MK_REP having an average salary of 6000)
* Summaries by departments (for example, 20 and a NULL job title, with an av
erage salary of 9500)
* A grand total (NULL department number and NULL job title, with an average
salary for all employees in all departments of 6250)

You might also like