select * from (
select id, dept,deptCode,projCode,
prpjdepartname,
cost_type,
cost_type_id,budget_year,
budget_money,
budget_money_add,
budget_moneyhj
from w_fm_budget
union
select null,t.dept,deptCode,projCode,
t.prpjdepartname,
'项目部预算合计',null,budget_year,
sum(t.budget_money),
sum(t.budget_money_add),
sum(t.budget_moneyhj)
from w_fm_budget t
group by t.dept, t.prpjdepartname,deptCode,projCode,budget_year
union
select null,t.dept,deptCode,null,
null,
'费用归属部门预算合计',null,budget_year,
sum(t.budget_money),
sum(t.budget_money_add),
sum(t.budget_moneyhj)
from w_fm_budget t
group by t.dept,deptCode,budget_year
union
select null,null,null,null,
null,
'总预算额',null,t.budget_year,
sum(t.budget_money),
sum(t.budget_money_add),
sum(t.budget_moneyhj)
from w_fm_budget t
group by t.budget_year
)
where 1=1
and budget_year=2018
order by abs(deptCode),projCode,cost_type_id
结果如下: