The fourth day of SQL Learning -- about SQL with cube, with rollup and grouping

Source: Internet
Author: User
About with cube, with rollup and grouping

View the help document of SQL 2005 to find the specific difference between cube and rollup.:

  1. The result set generated by cube displays the aggregation of all the combinations of the values in the selected column.
  2. The result set generated by rollup displays the aggregation of a certain hierarchy of values in the selected column.

Let's look at the explanation of grouping:

When a row is added by the cube or rollup operator, this function causes the output value of the appended column to 1. When a row is not added by the cube or rollup operator, this function will cause the output value of the appended column to be 0.

Grouping is allowed only in the selection list associated with the group by clause that contains the cube or rollup operator.

When we see that the above explanation is very vague, and Alibaba Cloud does not know how to use it, we will try it through instance operations below:

Create a table first (DBO. leleinfo ):

CREATE TABLE [dbo].[PeopleInfo](    [id] [int] IDENTITY(1,1) NOT NULL,    [name] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,    [numb] [nchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,    [phone] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,    [FenShu] [int] NULL) ON [PRIMARY]

Insert data to a table:

Insert into peopleinfo ([name], numb, phone, fenshu) values ('Li Huan ', '000000', '000000', 80) insert into peopleinfo ([name], numb, phone, fenshu) values ('Li Huan ', '000000', '1', 90) insert into peopleinfo ([name], numb, phone, fenshu) values ('Lee name', '123', '123', 56) insert into peopleinfo ([name], numb, phone, fenshu) values ('Lee name ', '123', '123', 60) insert into peopleinfo ([name], numb, phone, fenshu) values ('wang hua', '123', '123', 80)

Query all the inserted data:

select * from dbo.PeopleInfo

Result

Operation 1: First try: 1. query all data; 2. Use group by to query all data; 3. Use with cube. Comparison of the three cases

The SQL statement is as follows:

Select * From DBO. leleinfo -- 1, query all data; select [name], numb, sum (fenshu) from DBO. peopleinfo group by [name], numb -- 2, Use group by to query all data; select [name], numb, sum (fenshu) from DBO. peopleinfo group by [name], numb with cube -- 3, with cube. Comparison of the three cases

Result

Result Analysis:

Why does the third (with cube) show null field values? By analyzing the combination of values on the graph, we can find out what happened. Take the third data (Li Huan, null, 170) as an example: it only divides the name "Li Huan" into a group without considering "Numb". Therefore, the third piece of data is added, which also shows how 170 came from. This is also true for others. Let's review the help document's explanation: the result set generated by the cube shows the aggregation of all the combinations of the values in the selected column, and many of them are found.

Operation 2:1. Use with cube; 2, and use with Rollup for comparison

The SQL statement is as follows:

Select [name], numb, sum (fenshu) from DBO. peopleinfo group by [name], numb with cube -- use with cube. Select [name], numb, sum (fenshu) from DBO. peopleinfo group by [name], numb with rollup -- use with rollup.

Result

Result Analysis:

Why is with cube more than with rollup? It is not displayed in the past. It is grouped by [numb] without considering the data of [name. Let's review the help document: the result set generated by rollup displays the aggregation of a certain hierarchy of values in the selected column, so what is the standard for this [level?My guess is: The field closest to group up must be considered in the group.

Example of proof conjecture:

Operation: compare an SQL statement that exchanges fields with two group up statements and an SQL statement that adds a field after group up:

The SQL statement is as follows:

 

select [name],numb from dbo.PeopleInfo group by [name],numb with rollupselect [name],numb from dbo.PeopleInfo group by numb,[name] with rollupselect [name],numb,phone from dbo.PeopleInfo group by [name],numb,phone with rollup

 

Result

The result graph shows that the conjecture is correct.

---------------------------------------------------Grouping-------------------------------------------------

Now let's take a look at the grouping instance:

Check the combination of SQL statements and with rollup (the same as with cube ):

select [name],numb,grouping(numb) from dbo.PeopleInfo group by [name],numb with rollup

Result

Result Analysis:

Combined with the help documentation: When a row is added by the cube or rollup operator, this function will cause the output value of the appended column to 1. When a row is not added by the cube or rollup operator, this function will cause the output value of the appended column to be 0. It's easy to understand.

 

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.