Rollup与group by组合使用,可对分组结果进行进一步的汇总。
创建数据表
select * from emp_test
(1) rollup单个字段
如按照country字段进行分组,并在最后追加一条汇总所有country的值
select GENDER,COUNTRY,count(1) from emp_test group by rollup (COUNTRY)
(2) rollup多个字段
按照gender,country字段进行分组,并针对每一个country追加一条汇总值,并在最后追加一条所有country汇总
select GENDER,COUNTRY,count(1) from emp_test group by rollup (COUNTRY,GENDER)
rollup后面的字段顺序不同,结果也不同,取决于第一个字段,如:
select GENDER,COUNTRY,count(1) from emp_test group by rollup (GENDER,COUNTRY)
转自:https://www.cnblogs.com/shiliye/p/10529308.html