Oracle grouping function rollup, cube

Source: Internet
Author: User

Oracle grouping function rollup, cube
Rollup (Field 1, Field 2 ):
Cube (Field 1, Field 2 ):
Rollup (col1, col2 ,...) and cube (col1, col2 ,...) the usage difference lies in the cube in the record set summarized by rollup, and the summary of fields such as col2 is also added;
Www.2cto.com ROLLUP only summarizes the first parameter (field). CUBE can summarize the parameters (fields) in sequence. Therefore, only one parameter in ROLLUP takes effect (and the parameter is ranked first ).
In addition to the most basic syntax, Oracle group by statements also support ROLLUP and CUBE statements.
ROLLUP (A, B, C ):
First, group by is performed on (A, B, C), then group by is performed on (A, B), and then GROUP BY is performed on (, finally, perform the group by operation on the entire table.
CUBE (A, B, C), group by (A, B, C) is first performed, followed BY (A, B), (A, C ), (A), (B, C), (B), (C), and finally perform the GROUP BY operation on the entire table. The difference between CUBE and ROLLUP is:
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. Example: [SQL] create table student (cgrade varchar2 (64), cclass varchar2 (64), cgroup varchar2 (64), stu int) [SQL] insert into student (cgrade, cclass, cgroup, stu) values ('1', '1', '1', 10); insert into student (cgrade, cclass, cgroup, stu) values ('1', '2', '1', 10); insert into student (cgrade, cclass, cgroup, stu) values ('1', '2 ', '2', 20); insert into student (cgrade, cclass, cgroup, stu) values ('2', '1', '1', 30 ); insert into student (cgrade, cclass, cgroup, stu) values ('2', '2', '2', 40); [SQL] select * from student; [SQL] select cgrade, cclass, sum (stu) from student group by cgrade, cclass; [SQL] select cgrade, cclass, sum (stu) from student group by cube (cgrade, cclass); [SQL] select decode (grouping (cgrade), 1, 'Students, 0, cgrade), decode (grouping (cclass) + grouping (cgrade), 1, 'Number of grades', 0, cclass), sum (stu) from student group by rollup (cgrade, cclass );
 

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.