max(line) , max(title_code) ,
sum(headcount) as Headcounts
from #tmpA
group by dept , sect with rollup
sect sect Headcounts
--------------- -------------------- ---- ---- -----------
DA S1 Lb T2 12
DA S2 Lb T1 6
DA total ( by Dept ) : Lb T2 18
DB S1 Lb T1 3
DB S2 Lb T1 6
DB total ( by Dept ) : Lb T1 9
DC S1 La T1 3
DC S2 Lb T1 3
DC total ( by Dept ) : Lb T1 6
Grant total : Lb T2 33