declare @t table
(StudentName nvarchar(20),
Subject nvarchar(20),
Score int)
Insert into @t (StudentName,Subject,Score) values ( '学生A', '中文', 80 );
Insert into @t (StudentName,Subject,Score) values ( '学生A', '数学', 78 );
Insert into @t (StudentName,Subject,Score) values ( '学生A', '英语', 92 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '中文', 89 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '数学', 87 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '英语', 75 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '中文', 92 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '数学', 74 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '英语', 65 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '中文', 79 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '数学', 83 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '英语', 81 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '中文', 73 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '数学', 84 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '英语', 93 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '中文', 79 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '数学', 86 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '英语', 84 );
select StudentName,
sum(case when Subject = N'中文' then Score else 0 end) Chinese,
sum(case when Subject = N'数学' then Score else 0 end) Math,
sum(case when Subject = N'英语' then Score else 0 end) Engilsh
from @t
group by StudentName