Total Sum of oracle -- rollup and cube groups

Source: Internet
Author: User
The groupby Statement supports basic having conditions and provides information aggregation functions for rollup and cube, similar to subtotal. Rollup: vertical subtotal, which summarizes each column one by one from right to left and generates an independent row in the result. Only the statistical row of the column specified by the first grouping condition is returned. Cube: horizontal subtotal selectd. deptno, t. job, sum (t. sal)

The group by Statement supports basic having conditions and provides information aggregation functions for rollup and cube, similar to subtotal. Rollup: vertical subtotal, which summarizes each column one by one from right to left and generates an independent row in the result. Only the statistical row of the column specified by the first grouping condition is returned. Cube: horizontal subtotal selectd. deptno, t. job, sum (t. sal)

The group by Statement supports basic having conditions and provides information aggregation functions for rollup and cube, similar to subtotal.

Rollup: vertical subtotal, which summarizes each column one by one from right to left and generates an independent row in the result. Only the statistical row of the column specified by the first grouping condition is returned.

Cube: Subtotal

  1. Select d. deptno, t. job, sum (t. sal) from emp t, dept d
  2. Where t. deptno = d. deptno
  3. Group by rollup (d. deptno, t. job)

Execution result:

***

Use grouping_id to display records at the specified group level. Returns the decimal value of the grouping () bit vector. The calculation method of the GROUPING bit vector is to combine the results of calling the GROUPING function for each column in order.

For example: grouping_id (a, B, c). If a is null, 0 is non-null, and B is the same as c. a three-digit number is returned, which is converted to decimal by binary, a, B, and c are all non-empty, that is, 111 is 7, and three columns are 7. If they are two columns, 11 is 3.

Select... from Table group by rollup (...) having grouping_id (...) <= 1

Rollup (x, y) Columns Rollup (x, y, z) 3 columns
Total is grouping_id = 3 Total is grouping_id = 7
Subtotal grouping_id = 1 Subtotal grouping_id = 3
The record is grouping_id = 0 The record is grouping_id = 0

In this case, do not display the last total line:

  1. Select d. deptno, t. job, sum (t. sal) from emp t, dept d
  2. Where t. deptno = d. deptno
  3. Group by rollup (d. deptno, t. job) having grouping_id (d. deptno, t. job) <= 1

Execution result:

In actual situations, we need to add subtotal and total to the summary columns in the result set. In this case:

Execution result:

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.