[code]create table test1(name varchar(20),fenlei varchar(20),fenshu numeric(19,6))
insert into test1
select '张三','数学',50
union all
select '张三','语文',60
union all
select '李四','数学',70
select * from test1select t0.name,max(case t0.fenlei when '数学' then t0.fenshu else 0 end) 数学,
               max(case t0.fenlei when '语文' then t0.fenshu else 0 end) 语文
       from test1 t0
       group by t0.name
                                  
declare @sql varchar(2000)
select @sql ='select name'
select @sql = @sql+ ',max(case fenlei when '''+fenlei+''' then fenshu else 0 end) ['+fenlei+']' from test1 group by fenlei
select @sql + '  from test1 group by name'print(@sql)
exec(@sql)