SQLSERVER 多行转列

今天在群里看到有人在讨论多行转一列的实现方法,下面是别人的方法,还没测试过,应该可行,先保存下,备用

参考页面:http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html

declare @Lessons varchar(1000)
declare @SqlTxt varchar(1000)
set @Lessons=''
select @Lessons=@Lessons+课程+',' from tb group by 课程
set @Lessons = SUBSTRING(@Lessons,0,len(@Lessons))
print @Lessons

set @SqlTxt = 'select * from tb pivot(max(分数) for 课程 in ('+@Lessons+')) a'
print @SqlTxt
exec(@SqlTxt)

前:

后(效果):

原文地址:https://www.cnblogs.com/reddptp/p/3413833.html