PLSQL_ Basic Series 2_ grouping function group By/rollup/cube

Source: Internet
Author: User

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 following are the specific differences between CUBE and ROLLUP:

The result set generated by cube shows aggregations for all combinations of values in the selected column.

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

2. ROLLUP Advantages:

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

(2). ROLLUP can be used in server cursors, while COMPUTE by is not possible.

(3). Sometimes, the query optimizer generates more efficient execution plans for ROLLUP than is generated for COMPUTE by.

Second, create test data

1. Create a profile

CREATE TABLEDEPARTMENT (DEPARTMENTCHAR(Ten), EMPLOYEECHAR(6), SALARYINT); BEGIN INSERT  intoDEPARTMENTSELECT 'A','ZHANG', -  fromDUAL;INSERT  intoDEPARTMENTSELECT 'A','LI', $  fromDUAL;INSERT  intoDEPARTMENTSELECT 'A','WANG', -  fromDUAL;INSERT  intoDEPARTMENTSELECT 'A','ZHAO', -  fromDUAL;INSERT  intoDEPARTMENTSELECT 'A','DUAN', -  fromDUAL;INSERT  intoDEPARTMENTSELECT 'B','DUAN', -  fromDUAL;INSERT  intoDEPARTMENTSELECT 'B','DUAN', the  fromDUAL;END;

2. View information

Three, rollup case

The rollup result set has more than three aggregated information: the total of department A, the total of department B, and the total sum. Which totals the Duan in Department b

Iv. Case of Cube

The result set of cube is 5 rows on the basis of the rollup result set, which is equivalent to the result of the GROUP by employees (that is, cube) on the rollup result set on the Union.

Thanks and regards

PLSQL_ Basic Series 2_ grouping function group By/rollup/cube

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.