declare @t table (部门 varchar(10),金额 int)
insert into @t
select '01',-123
union all select '01',123
union all select '02',-123
union all select '02',123
union all select '02',-123
union all select '03',123
union all select '03',-123
union all select '04',123
union all select '04',-123
select 部门,sum(金额) as 总和 from @t
group by 部门,(case when 金额 > 0 then 1 else 2 end)