Group By Rollup

Rollup与group by组合使用,可对分组结果进行进一步的汇总。

 

创建数据表

select * from emp_test

Group By Rollup

 

(1) rollup单个字段

如按照country字段进行分组,并在最后追加一条汇总所有country的值

select GENDER,COUNTRY,count(1)
from emp_test
group by rollup (COUNTRY)

Group By Rollup

 

(2) rollup多个字段

按照gender,country字段进行分组,并针对每一个country追加一条汇总值,并在最后追加一条所有country汇总

select GENDER,COUNTRY,count(1)
from emp_test
group by rollup (COUNTRY,GENDER)

Group By Rollup


rollup后面的字段顺序不同,结果也不同,取决于第一个字段,如:
select GENDER,COUNTRY,count(1)
from emp_test
group by rollup (GENDER,COUNTRY)

Group By Rollup

 

上一篇:(plugin uglify) Error: Unexpected token: keyword «const»


下一篇:db-在分组查询中使用ROLLUP