SQL server cube operators

Source: Internet
Author: User
Cube operator
To use cube, first understand group
In fact, the difference between cube and rollup is not big, but there are some differences in the possible combinations of group by clause creation and summary grouping,
Cube returns more possible combinations. If the group by clause contains n columns or n expressions,
Sqlserver returns the N-1 power combination of 2 in the result set.
Note:
The cube operator can contain up to 10 grouping expressions.
The all keyword cannot be used in cube.
Example:
We often query the following situations in database statistics:
For example, for an order database, we need to know the quantity of each product in each order, the quantity of all products in each order, the quantity of a product in all orders, and the total quantity of all products in all orders. Using cube is very convenient. Of course, if you don't need so much information or want to know a specific product, a specific order, a certain time relationship (between the beginning, the end, and the end), and so on, you only need to limit it in where.
The following is an example of all situations:
A database table records a product order:
There are currently three products (1, 2, 3), and two orders (1, 2) have been placed)
SQL statement:
Select productid, orderid SUM (quantity) AS total FROM order group by productid, orderid WITH CUBE
Order by productid, orderid
The result is as follows:
Productid orderid total
Null 95 total number of all products in all orders
Null 1 30 Order 1 number of all products
Null 2 65 order 2 Number of all products
1 null 15 total number of products under all orders 1
1 1 5 orders 1 products 1 quantity
1 2 10 orders 2 products 1 quantity
2 null 35 all orders product 2 total
2 1 10 orders 1 product 2 Quantity
2 2 25 orders 2 product 2 Quantity
3 null 45 all order products 3 total
3 1 15 order 1 product 3 Quantity
3 2 30 orders 2 products 3 Quantity
If you have some understanding of sqlserver group by clustering, you can understand the usage and function of the cube operator. In fact, cubes are quite good in practical use. We often need to make statistics on some database data to help us better understand the situation.
I think e-commerce can better achieve reasonable resource allocation and minimize inventory. Only by better understanding the specific conditions of production and sales data can we achieve reasonable resource allocation. We hope that enterprises in the future will not be like color TV manufacturers such as Changhong like dell.


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.