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