create table t
(项目 varchar(3),部门 varchar(2),金额 int)insert t
select '001','A',100 union all
select '001','B',200 union all
select '001','A',50 union all
select '002','A',60 union all
select '002','B',40 union all
select '002','B',30
godeclare @sql varchar(2000)
set @sql='select 项目'
select @sql=@sql+',[部门'+部门+']=isnull(sum(case when 部门='''+部门+''' then 金额 end),0)'
from t
group by 部门
select @sql=@sql+' from t group by 项目 order by 项目'
exec(@sql)drop table t
项目   部门A         部门B         
---- ----------- ----------- 
001  150         200
002  60          70警告: 聚合或其它 SET 操作消除了空值。