DB2 当中Rollup得用法,得一些看法。
select DEPARTMENT,Name,sum(amount) as sum,count(*) as count
from sale
inner join Employee on liaohaibing.EMPLOYEE.ID = liaohaibing.SALE.EMPLOYEEID
inner join liaohaibing.DEPARTMENT on
liaohaibing.DEPARTMENT.DEPARTMENTID = liaohaibing.EMPLOYEE.DEPARTMENTID
where liaohaibing.DEPARTMENT.DEPARTMENTID in(2,4,5,9)
group by rollup(DEPARTMENT,Name);
from sale
inner join Employee on liaohaibing.EMPLOYEE.ID = liaohaibing.SALE.EMPLOYEEID
inner join liaohaibing.DEPARTMENT on
liaohaibing.DEPARTMENT.DEPARTMENTID = liaohaibing.EMPLOYEE.DEPARTMENTID
where liaohaibing.DEPARTMENT.DEPARTMENTID in(2,4,5,9)
group by rollup(DEPARTMENT,Name);
显示出来得结果如下表所示.
DEPARTMENT | NAME | SUM | COUNT |
184780 | 128 | ||
后勤部门 | 58554 | 48 | |
开发部门 | 48625 | 32 | |
资产管理部门 | 77601 | 48 | |
后勤部门 | fff | 15033 | 16 |
后勤部门 | ggg | 18000 | 16 |
后勤部门 | qqq | 25521 | 16 |
开发部门 | lhb | 26293 | 16 |
开发部门 | liaohaibing | 22332 | 16 |
资产管理部门 | ccc | 28936 | 16 |
资产管理部门 | ddd | 28033 | 16 |
资产管理部门 | eee | 20632 | 16 |
Rollup
GROUP BY ROLLUP(DEPARTMENT,NAME)
第一是按把所有和显示到第一行。
第二是按GROUP BY DEPARTMENT 这种情况显示出来,这里是按部门进行分组。
第三是按GROUP BY DEPARTMENT,NAME情况显示出来,这里是按某部门中得名字进行分组得。