一、需求
由于表达能力不好,直接上图。在mssql环境中需要把图1的表转化成图2的表:
301/2013010816511393.jpg"> 图1 301/2013010816511394.png">图2
二、分析
301/2013010816511395.png">
2.1创建表
代码如下:
create table #tb
(ID int primary key,
Class nvarchar(max),
[Subject] nvarchar(max),
score int)
insert into #tb values(1,'一(1)班','语文','92')
insert into #tb values(2,'数学','91')
insert into #tb values(3,'英语','93')
insert into #tb values(4,'一(2)班','94')
insert into #tb values(5,'95')
insert into #tb values(6,'96')
insert into #tb values(7,'一(3)班','94')
insert into #tb values(8,'96')
insert into #tb values(9,'97')
select * from #tb
(ID int primary key,
Class nvarchar(max),
[Subject] nvarchar(max),
score int)
insert into #tb values(1,'一(1)班','语文','92')
insert into #tb values(2,'数学','91')
insert into #tb values(3,'英语','93')
insert into #tb values(4,'一(2)班','94')
insert into #tb values(5,'95')
insert into #tb values(6,'96')
insert into #tb values(7,'一(3)班','94')
insert into #tb values(8,'96')
insert into #tb values(9,'97')
select * from #tb
2.2把图3中的第一张表转化为第二张表
代码如下:
2.3把图3中的第二张表转化为第三张表
代码如下:
--观察第二张表,表显然很多成绩是错误的,所以要消除错误 --消除错误的方法:先把错误的成绩设置为0,然后每一行取最大的数作为成绩,并按照科目来分组显示
select subject,[一(1)班]=case when [Class]='一(1)班' then [score] else 0 end
,[一(2)班]= case when [Class]='一(2)班' then [score] else 0 end
,[一(3)班]=case when [Class]='一(3)班' then [score] else 0 end
from #tb
select subject,[一(1)班]=case when [Class]='一(1)班' then [score] else 0 end
,[一(2)班]= case when [Class]='一(2)班' then [score] else 0 end
,[一(3)班]=case when [Class]='一(3)班' then [score] else 0 end
from #tb
2.4 把图3中的第三张表转化为第四张表
代码如下:
select subject,[一(1)班]=max( case when [Class]='一(1)班' then [score] else 0 end)
,[一(2)班]=max( case when [Class]='一(2)班' then [score] else 0 end)
,[一(3)班]=max( case when [Class]='一(3)班' then [score] else 0 end)
from #tb group by subject
,[一(2)班]=max( case when [Class]='一(2)班' then [score] else 0 end)
,[一(3)班]=max( case when [Class]='一(3)班' then [score] else 0 end)
from #tb group by subject
2.5简化上面的写法
代码如下:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(Class)+'=max( case when [Class]='+quotename(Class,'''')+' then [score] else 0 end)'
from #tb group by Class
--print @s
exec('select [Subject]'+@s+' from #tb group by [Subject]')
set @s=''
Select @s=@s+','+quotename(Class)+'=max( case when [Class]='+quotename(Class,'''')+' then [score] else 0 end)'
from #tb group by Class
--print @s
exec('select [Subject]'+@s+' from #tb group by [Subject]')
三、简化分析和需求延伸3.1简化分析
上面的简化,对于菜鸟级的我,理解着还是非常吃力的,所以我是在知道答案后,用 print @s一步一步的去分析的,特别是在select @s=@s+.....的时间,在这里我找到了一种把单列数据连接成字符串的方法。
301/2013010816511396.png">
3.2需求延伸
或许上面的需求太简单,那么我们现在引出复杂点的需求,动态获取班级的成绩,比如我们的需求可能是列出部分班级的成绩,代码如下:
代码如下:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','''')+' then [score] else 0 end)'
from #tb
where Class in(你想要的班级)
group by Class
--print @s
exec('select [Subject]'+@s+' from #tb group by [Subject]')
set @s=''
Select @s=@s+','''')+' then [score] else 0 end)'
from #tb
where Class in(你想要的班级)
group by Class
--print @s
exec('select [Subject]'+@s+' from #tb group by [Subject]')