create table tb(name varchar(10),subject varchar(10),mark int) insert into tb values('A', '语文', 80) insert into tb values('A', '数学', 80) insert into tb values('A', '外语', 80 ) insert into tb values('B', '数学', 80) insert into tb values('B', '外语', 80) insert into tb values('C', '语文', 78) select name , max(case subject when '语文' then mark else 0 end) '语文', max(case subject when '数学' then mark else 0 end) '数学', max(case subject when '外语' then mark else 0 end) '外语', sum(mark)/(select max(cnt) cnt from (select name , count(*) cnt from tb group by name) t) [avg] from tb group by name declare @sql varchar(8000) set @sql = 'select Name as ' + '姓名' select @sql = @sql + ' , sum(case Subject when ''' + Subject + ''' then mark else 0 end) [' + Subject + ']' from (select distinct Subject from tb) as a set @sql = @sql + ' ,sum(mark)/(select max(cnt) cnt from (select name , count(*) cnt from tb group by name) t) [avg] from tb group by name' exec(@sql)