create table t
(类别 varchar(20),日期 datetime,数量 int)insert t
select '00','2006-1-2',23 union all
select '01','2006-1-2',13 union all
select '20','2006-1-2',9 union all
select '00','2006-1-3',3 union all
select '01','2006-1-3',2 union all
select '30','2006-1-3',23 union all
select '00','2006-1-4',33 union all
select '10','2006-1-4',53declare @sql varchar(8000),@t datetime
set @t=getdate() --获取执行前时间
set @sql='select 日期'
select @sql=@sql+',['+类别+']=isnull(sum(case when 类别='''+类别+''' then 数量 end),0)'
from t group by 类别 order by 类别
select @sql=@sql+' from t group by 日期'
exec(@sql)select datediff(ms,@t,getdate()) ----取得执行时间
drop table t