请教一个sql统计的问题

select dep,depid,
sum(case when company ='DHL' then cost else 0 end)DHL,
sum(case when company ='EMS' then cost else 0 end)EMS,
sum(case when company ='TNT' then cost else 0 end)TNT,
sum(case when company ='SF' then cost else 0 end)SF,
sum(cost)cost
FROM #test group by dep,depid
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME([company]) + '=max(case when [company]='
+ QUOTENAME([company], '''') + ' then [cost] else 0 end)'
FROM #test
GROUP BY [company]
EXEC('select dep,depid'+@s+' from #test group by dep,depid')