sqlserver行列的转换
SELECT * FROM Table_Test
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'from (select distinct Subject from Table_Test) as a
set @sql = @sql + ' from Table_Test group by name'
exec(@sql)
--第二种方法
select name
,sum(case subject when'数学' then Result else 0 end ) as '数学'
,sum(case subject when'英语' then Result else 0 end ) as '英语'
,sum(case subject when'语文' then Result else 0 end ) as '语文'
from Table_Test
group by name