使用 GROUP BY WITH ROLLUP 改善统计性能

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

使用 GROUP BY 的 WITH ROLLUP 字句可以检索出更多的分组聚合信息,它不仅仅能像一般的 GROUP BY 语句那样检索出各组的聚合信息,还能检索出本组类的整体聚合信息。

下面我们的例子对比了普通的 GROUP BY 操作和有 WITH ROLLUP 子句的 GROUP BY 操作的不同:

查询表的内容,是雇员的基础信息表:


  1. mysql> 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. 10 rows in set (0.00 sec)  
mysql> select * from employee;+------+--------+------+------+------+| id | name | dep | pos | sal |+------+--------+------+------+------+| 1 | abcd | 01 | 01 | 1000 || 2 | eefs | 01 | 02 | 2000 || 3 | micro | 02 | 01 | 1500 || 4 | cathey | 02 | 02 | 3000 || 5 | amy | 03 | 01 | 2500 || 6 | lily | 03 | 02 | 2200 || 7 | bobo | 01 | 01 | 2000 || 8 | gray | 01 | 02 | 1900 || 9 | leon | 03 | 02 | 2900 || 10 | sun | 02 | 02 | 1900 |+------+--------+------+------+------+10 rows in set (0.00 sec)  

普通的 GROUP BY 操作,可以按照部门和职位进行分组,计算每个部门,每个职位的工资平均值:


  1. mysql> select dep,pos,avg(sal) from 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)  
mysql> select dep,pos,avg(sal) from employee group by dep,pos;+------+------+-----------+| dep | pos | avg(sal) |+------+------+-----------+| 01 | 01 | 1500.0000 || 01 | 02 | 1950.0000 || 02 | 01 | 1500.0000 || 02 | 02 | 2450.0000 || 03 | 01 | 2500.0000 || 03 | 02 | 2550.0000 |+------+------+-----------+6 rows in set (0.02 sec)  


如果我们希望再显示部门的平均值和全部雇员的平均值,普通的 GROUP BY 语句是不能实现的,需要另外执行一个查询操作,或者通过程序来计算。如果使用有 WITH ROLLUP 子句的 GROUP BY 语句,则可以轻松实现这个要求:


  1. mysql> select dep,pos,avg(sal) from 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. 10 rows in set (0.00 sec)  
mysql> select dep,pos,avg(sal) from employee group by dep,pos with rollup;+------+------+-----------+| dep | pos | avg(sal) |+------+------+-----------+| 01 | 01 | 1500.0000 || 01 | 02 | 1950.0000 || 01 | NULL | 1725.0000 || 02 | 01 | 1500.0000 || 02 | 02 | 2450.0000 || 02 | NULL | 2133.3333 || 03 | 01 | 2500.0000 || 03 | 02 | 2550.0000 || 03 | NULL | 2533.3333 || NULL | NULL | 2090.0000 |+------+------+-----------+10 rows in set (0.00 sec)  


需要注意的是,使用有 WITH ROLLUP 子句的 GROUP BY 语句时,不能再使用 ORDER BY 语句对结果集进行排序,如果对返回的结果顺序不满意,需要应用程序获得结果后在程序中进行排序。


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/gruhgd/article/details/83895984