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