col descr for a20
--■■■rollupを使う方法■■■
select case grouping_ID(deptno, empno)
when 0+1 then 'Total Of dept - ' || deptno
when 2+1 then 'Grand Total of dept'
else ' ' end descr,
deptno,empno,sum(sal) as sal
from copyEmp
group by rollup(deptno, empno)
order by deptno, empno;
--■■■model句を使う方法1(10g以降)■■■
select descr,deptno,empno,sal
from copyEmp
model
dimension by(RowNum as rn,deptno)
measures(cast(' ' as varChar2(20)) as descr,empno,sal)
rules(
upsert all descr[0,any] = 'Total Of dept - ' || cv(deptno),
descr[null,null] = 'Grand Total of dept',
sal[null,null] = sum(sal)[any,any],
sal[0,any] order by rn,deptno = sum(sal)[any,cv()])
order by deptno,empno;
--■■■model句を使う方法2(10g以降)■■■
select descr,deptno,empno,sal
from copyEmp
model
dimension by(RowNum as rn,deptno)
measures(cast(' ' as varChar2(20)) as descr,empno,sal)
rules(
upsert all descr[0,any] = 'Total Of dept - ' || cv(deptno),
descr[null,null] = 'Grand Total of dept',
sal[0,any] order by rn,deptno = sum(sal)[any,cv()],
sal[null,null] = sum(sal)[0,any])
order by deptno,empno;