Group By Rollup

Rollup與group by組合使用,可對分組結果進行進一步的彙總。spa

 

建立數據表3d

select * from emp_test

 

(1) rollup單個字段code

如按照country字段進行分組,並在最後追加一條彙總全部country的值blog

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

 

(2) rollup多個字段class

按照gender,country字段進行分組,並針對每個country追加一條彙總值,並在最後追加一條全部country彙總test

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)

相關文章
相關標籤/搜索