Use of ORACLEROLLUP and CUBE

Source: Internet
Author: User
ROLLUP is an extension of the GROUPBY clause. It can return subtotal records for each group and total records for all groups. CUBE is an extension of the GROUPBY clause. It returns the subtotal record of each column combination and adds a total record at the end. 1. Pass a column of SQLselectdivision_id and sum (salary) 2f to ROLLUP

ROLLUP is an extension of the group by clause. It can return subtotal records for each GROUP and total records for all groups. CUBE is an extension of the group by clause. It returns the subtotal record of each column combination and adds a total record at the end. 1. Pass a column of SQL select pision_id, sum (salary) 2 f to ROLLUP

ROLLUP is an extension of the group by clause. It can return subtotal records for each GROUP and total records for all groups.

CUBE is an extension of the group by clause. It returns the subtotal record of each column combination and adds a total record at the end.

1. Pass a column to ROLLUP

SQL> select pision_id, sum (salary)
2 from employees2
3 group by rollup (pision_id)
4 order by pision_id;

Div sum (SALARY)
--------------
BUS 1, 1610000
OPE 1320000
SAL 1, 4936000
SUP 1015000
8881000

SQL>

Let's take a look at what happens if you use normal group by without ROLLUP.

SQL> select pision_id, sum (salary)
2 from employees2
3 group by pision_id
4 order by pision_id;

Div sum (SALARY)
--------------
BUS 1, 1610000
OPE 1320000
SAL 1, 4936000
SUP 1015000

The final statistics are missing.

2. Pass multiple columns to ROLLUP

SQL> select pision_id, job_id, sum (salary)
2 from employees2
3 group by rollup (pision_id, job_id)
4 order by pision_id, job_id;

Div job sum (SALARY)
-----------------
Bus mgr 530000
Bus pre 800000
Bus wor 280000
BUS 1, 1610000
Ope eng 245000
Ope mgr 805000
Ope wor 270000
OPE 1320000
Sal mgr 4446000
SAL wand 490000
SAL 1, 4936000

Div job sum (SALARY)
-----------------
Sup mgr 465000
Sup tec 115000
Sup wor 435000
SUP 1015000
8881000

16 rows selected.

As you can see, in addition to the last sum record, each pision_id group also has a sum record.

Now let's switch the sequence of Data columns in ROLLUP to see how the result works.

SQL> select job_id, pision_id, sum (salary)
2 from employees2
3 group by rollup (job_id, pision_id)
4 order by job_id, pision_id;

Job div sum (SALARY)
-----------------
Eng ope 245000
ENG 245000
Mgr bus 530000
Mgr ope 805000
Mgr sal 4446000
Mgr sup 465000
MGR 1, 6246000
Pre bus 800000
Pre800000
Tec sup 115000
TEC 115000

Job div sum (SALARY)
-----------------
Wor bus 280000
Wor ope 270000.
Wor sal 490000.
Wor sup 435000.
WOR 1475000
8881000

17 rows selected.

The result is similar, but every job_id group has a sum record.

3. Pass a column to the CUBE

SQL> select pision_id, sum (salary)
2 from employees2
3 group by cube (pision_id)
4 order by pision_id;

Div sum (SALARY)
--------------
BUS 1, 1610000
OPE 1320000
SAL 1, 4936000
SUP 1015000
8881000

It seems that it is no different from ROLLUP.

4. Pass multiple columns to the CUBE

SQL> select job_id, pision_id, sum (salary)
2 from employees2
3 group by cube (job_id, pision_id)
4 order by job_id, pision_id;

Job div sum (SALARY)
-----------------
Eng ope 245000
ENG 245000
Mgr bus 530000
Mgr ope 805000
Mgr sal 4446000
Mgr sup 465000
MGR 1, 6246000
Pre bus 800000
Pre800000
Tec sup 115000
TEC 115000

Job div sum (SALARY)
-----------------
Wor bus 280000
Wor ope 270000.
Wor sal 490000.
Wor sup 435000.
WOR 1475000
BUS 1, 1610000
OPE 1320000
SAL 1, 4936000
SUP 1015000
8881000

21 rows selected.

We can see that the salary is summed according to job_id and pision_id. CUBE returns a record in every job_id, indicating the total salary, and displays the total salary of each kind of pision_id near the end, the last record shows the total number of all salaries.

What if I change the order of the two columns? If you are interested, try it yourself.

========================================================== ========================================================== =

Create table pisions (
Pision_id CHAR (3) CONSTRAINT pisions_pk primary key,
Name VARCHAR2 (15) NOT NULL
);

Create table jobs (
Job_id CHAR (3) CONSTRAINT jobs_pk primary key,
Name VARCHAR2 (20) NOT NULL
);

Create table employees2 (
Employee_id integer constraint employees2_pk primary key,
Pision_id CHAR (3)
CONSTRAINT employees2_fk_pisions
REFERENCES pisions (pision_id ),
Job_id CHAR (3) REFERENCES jobs (job_id ),
First_name VARCHAR2 (10) not null,
Last_name VARCHAR2 (10) not null,
Salary NUMBER (6, 0)
);

Insert into pisions (
Pision_id, name
) VALUES (
'Sal', 'sales'
);

Insert into pisions (
Pision_id, name
) VALUES (
'Ope', 'operations'
);

Insert into pisions (
Pision_id, name
) VALUES (
'Up', 'support'
);

Insert into pisions (
Pision_id, name
) VALUES (
'Bus', 'business'
);
Insert into jobs (
Job_id, name
) VALUES (
'Wor', 'worker'
);

Insert into jobs (
Job_id, name
) VALUES (
'Mgr ', 'manager'
);

Insert into jobs (
Job_id, name
) VALUES (
'Eng', 'engineer'
);

Insert into jobs (
Job_id, name
) VALUES (
'Tec ', 'regionlist'
);

Insert into jobs (
Job_id, name
) VALUES (
'Pre', 'President'
);
Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
1, 'bus', 'pre', 'James ', 'Smith', 800000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
2, 'sal', 'mgr ', 'ron', 'johnson, 350000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
3, 'sal ', 'wor', 'fred', 'hobbs ', 140000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
4, 'sup', 'mgr ', 'Susan', 'Jones, 200000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
5, 'sal', 'wor', 'rob', 'green', 350000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
6, 'sup', 'wor', 'jar', 'Brown, 200000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
7, 'up', 'mgr ', 'john', 'grey', 265000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
8, 'sup', 'wor', 'Jean ', 'blue, 110000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
9, 'sup', 'wor', 'henry', 'heyson', 125000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
10, 'ope ', 'mgr', 'kevin ', 'black', 225000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
11, 'ope ', 'mgr', 'keith ', 'long', 165000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
12, 'ope ', 'wor', 'frank', 'Howard', 125000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
13, 'ope', 'wor', 'doreen', 'penn', 145000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
14, 'bus', 'mgr ', 'mark', 'Smith, 155000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
15, 'bus', 'mgr ', 'jill', 'Jones, 175000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
16, 'ope ', 'eng', 'megan ', 'Craig', 245000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
17, 'sup', 'tec ', 'Matthew', 'brant', 115000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
18, 'ope ', 'mgr', 'Tony ', 'clerke', 200000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
19, 'bus', 'mgr ', 'tanya', 'fairway, 200000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
20, 'ope ', 'mgr', 'terry ', 'cliff', 215000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
21, 'sal', 'mgr, 'Steve ', 'green', 275000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
22, 'sal', 'mgr ', 'roy', 'red', 375000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
23, 'sal', 'mgr ', 'Sandra', 'Smith, 335000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
24, 'sal', 'mgr ', 'Gail', 'sil', 225000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
25, 'sal', 'mgr ', 'Gerald', 'gold', 245000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
26, 'sal', 'mgr ', 'eileen', 'lane, 235000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
27, 'sal', 'mgr ', 'doreen', 'upton', 235000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
28, 'sal', 'mgr ', 'jack', 'ewing, 235000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
29, 'sal', 'mgr ', 'Paul', 'owners', 245000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
30, 'sal', 'mgr ', 'manual', 'York, 255000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
31, 'sal', 'mgr ', 'trace', 'yellow', 225000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
32, 'sal', 'mgr ', 'sara', 'white', 235000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
33, 'sal', 'mgr ', 'terry', 'Iron, 225000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
34, 'sal', 'mgr ', 'Christine', 'Brown, 247000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
35, 'sal', 'mgr ', 'john', 'Brown, 249000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
36, 'sal', 'mgr ', 'kelvin', 'trenton', 255000
);

Insert into employees2 (
Employee_id, pision_id, job_id, first_name, last_name, salary
) VALUES (
37, 'bus', 'wor', 'dbin', 'Jones, 280000
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.