一、建表测试

create table StuScore(name varchar(38),Chinese decimal(4,1),mathematics decimal(4,1),English decimal(4,1))
insert into StuScore VALUES('张三',82.2,93,90)
insert into StuScore VALUES('李四',87,81.1,99)
insert into StuScore VALUES('王五',93,86.6,90.9)

image.png

二、列转行

select name,cource,score
from
	(select name,Chinese as '语文',mathematics as '数学',English as '英语' from StuScore) s
unpivot
	(score for cource in
		(语文,数学,英语)
) as StuInfo;

image.png

注意

数据库兼容级别低于2005提示以下信息:'unpivot' 附近有语法错误。您可能需要将当前数据库的兼容级别设置为更高的值,以启用此功能。有关 ALTER DATABASE 的 SET COMPATIBILITY_LEVEL 选项的信息,请参见帮助。

image.png