MYSQL with ROLLUP

Source: Internet
Author: User

With the group by, the with ROLLUP sentence can be used to retrieve more packet aggregation information, which not only can retrieve the aggregated information of each group as the general group by statement, but also can retrieve the aggregate information of the group class.

The following example compares the common group by operation with the group by operation with the ROLLUP clause:

The content of the query table is the employee's basic information table:

[XHTML]View PlainCopy < param name= "wmode" value= "Transparent" >
  1. <a href="HTTP://LIB.CSDN.NET/BASE/14" class=' Replace_word ' title="undefined" target= '_blank ' style=' color: #df3434; font-weight:bold; '  >mysql</a>> select * from Employee;
  2. +------+--------+------+------+------+
  3. | ID | name | DEP | POS | Sal |
  4. +------+--------+------+------+------+
  5. | 1 | ABCD | 01 | 01 | 1000 |
  6. | 2 | Eefs | 01 | 02 | 2000 |
  7. | 3 | Micro | 02 | 01 | 1500 |
  8. | 4 | Cathey | 02 | 02 | 3000 |
  9. | 5 | Amy | 03 | 01 | 2500 |
  10. | 6 | Lily | 03 | 02 | 2200 |
  11. | 7 | Bobo | 01 | 01 | 2000 |
  12. | 8 | Gray | 01 | 02 | 1900 |
  13. | 9 | Leon | 03 | 02 | 2900 |
  14. | 10 | Sun | 02 | 02 | 1900 |
  15. +------+--------+------+------+------+
  16. Rows in Set (0.00 sec)

A common group by operation that can be grouped by department and position to calculate the average wage for each department, each position:

[XHTML]View PlainCopy
  1. MySQL> select Dep,pos,avg (SAL) from the employee group by Dep,pos;
  2. +------+------+-----------+
  3. | DEP | POS | AVG (SAL) |
  4. +------+------+-----------+
  5. | 01 | 01 | 1500.0000 |
  6. | 01 | 02 | 1950.0000 |
  7. | 02 | 01 | 1500.0000 |
  8. | 02 | 02 | 2450.0000 |
  9. | 03 | 01 | 2500.0000 |
  10. | 03 | 02 | 2550.0000 |
  11. +------+------+-----------+
  12. 6 rows in Set (0.02 sec)

If we want to show the average of the department and the average of all employees again, the normal GROUP by statement is not possible, and you need to perform a separate query operation or a program to calculate. If you use the GROUP by statement with the WITH ROLLUP clause, you can easily implement this requirement:

[Java]View PlainCopy
  1. Mysql> Select Dep,pos,avg (SAL) from the employee GROUP by Dep,pos with rollup;
  2. +------+------+-----------+
  3. | DEP | POS | AVG (SAL) |
  4. +------+------+-----------+
  5. | 01 | 01 |  1500.0000 |
  6. | 01 | 02 |  1950.0000 |
  7. | 01 | NULL |  1725.0000 |
  8. | 02 | 01 |  1500.0000 |
  9. | 02 | 02 |  2450.0000 |
  10. | 02 | NULL |  2133.3333 |
  11. | 03 | 01 |  2500.0000 |
  12. | 03 | 02 |  2550.0000 |
  13. | 03 | NULL |  2533.3333 |
  14. | NULL | NULL |  2090.0000 |
  15. +------+------+-----------+
  16. Rows in Set (0.00 sec)

It is important to note that when using a GROUP by statement with the WITH ROLLUP clause, the result set can no longer be sorted by using the order BY statement, and if the order of the returned results is not satisfactory, it needs to be sorted in the program after the application obtains the results.

(go) MYSQL with ROLLUP

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.