PLSQL _ basic series 2 _ grouping function group by/ROLLUP/CUBE, plsqlrollup

Source: Internet
Author: User

PLSQL _ basic series 2 _ grouping function group by/ROLLUP/CUBE, plsqlrollup

2014-11-30 BaoXinjian

I. Summary

The result set generated by the ROLLUP operator is similar to the result set generated by the CUBE operator.

 

1. The specific differences between CUBE and ROLLUP are as follows:

The result set generated by CUBE displays the aggregation of all the combinations of the values in the selected column.

The result set generated by ROLLUP displays the aggregation of a certain hierarchy of values in the selected column.

 

2. Advantages of ROLLUP:

(1). ROLLUP returns a single result set, while compute by returns multiple result sets. Multiple result sets increase the complexity of application code.

(2). ROLLUP can be used in server cursors, whereas compute by cannot.

(3). Sometimes, the execution plan generated BY the query optimizer for ROLLUP is more efficient than that generated by compute.

 

Ii. Create Test Data

1. Create Materials

CREATE TABLE DEPARTMENT(   DEPARTMENT   CHAR (10),   EMPLOYEE     CHAR (6),   SALARY       INT); BEGIN INSERT INTO DEPARTMENT SELECT 'A','ZHANG',100 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'A','LI',   200 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'A','WANG', 300 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'A','ZHAO', 400 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'A','DUAN', 500 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'B','DUAN', 600 FROM DUAL; INSERT INTO DEPARTMENT SELECT 'B','DUAN', 700 FROM DUAL;END;

 

2. View information

 

Iii. ROLLUP case

The ROLLUP result contains three more summary information: department A's total, department B's total, and total. Total DUAN in department B

 

Iv. CUBE case

The CUBE result set contains five more rows based on the ROLLUP result set. These five rows are equivalent to the result that the employee (CUBE) is the group by on the ROLLUP result set on the union.

 

Thanks and Regards

Related Article

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.