日期:2014-05-17  浏览次数:20362 次

一道题目如下
本帖最后由 haiziguo 于 2013-01-07 20:39:24 编辑

怎么让表格如上图1,变成如下图所示的结果

创建图1的语句给出:

create table #tb
(ID int primary key,
Class nvarchar(max),
[Subject] nvarchar(max),
Score int)
insert into #tb values(1,'一(1)班','语文','98')
insert into #tb values(2,'一(1)班','数学','98')
insert into #tb values(3,'一(1)班','英语','98')
insert into #tb values(4,'一(2)班','数学','98')
insert into #tb values(5,'一(2)班','语文','98')
insert into #tb values(6,'一(2)班','英语','98')
insert into #tb values(7,'一(3)班','数学','98')
insert into #tb values(8,'一(3)班','语文','98')
insert into #tb values(9,'一(3)班','英语','98')
select * from #tb
sql

------解决方案--------------------
select Class,max(case [Subject] when 'chinese'then Score else '0'end) AS chinese,
max(case [Subject] when 'math'then Score else '0'end) AS math,
max(case [Subject] when 'english'then Score else '0'end) AS english from #tb group by Class