SQLServer如何处理数据集的维度变化

Student表有三列,分别是姓名、课程、成绩
Name Curricula Mark
张三 语文 70
李四 数学 80
王朝 英语 59
城南 马哲 70
王朝 语文 90

我想得到的效果是,列出各个学科及格的人名:
语文 化学 数学
张三 李四
王朝

学科不止3门,可能有八门怎么弄呢?其实这就是典型的维度方向变化.

准备数据:

create table stgrade(Name varchar(10), Curricula varchar(10) , Mark int);
go
insert into stgrade values('张三' , '语文','70' );
insert into stgrade values('李四' , '数学','80');
insert into stgrade values('王朝' , '英语','59');
insert into stgrade values('城南' , '马哲','70' );
insert into stgrade values('王朝' , '语文','90' );
go

select * from stgrade;

Name Curricula Mark
张三 语文 70
李四 数学 80
王朝 英语 59
城南 马哲 70
王朝 语文 90

用case when 来实现根据column的值来返回同一行别的column的值,因为我们这里不需要分数,只关心Curricula 和name,所以是在分数赛选过后,进行一个case when操作.

select
case when Curricula='语文' then name end 语文,
case when Curricula='数学' then name end 数学,
case when Curricula='英语' then name end 英语,
case when Curricula='马哲' then name end 马哲
from stgrade where mark>59

语文 数学 英语 马哲
张三 NULL NULL NULL
NULL 李四 NULL NULL
NULL NULL NULL 城南
王朝 NULL NULL NULL

又上面已经可以看到结果集合已经初具雏形了,要是更让每个column下的NULL消失,然后后面行的值补上来就能达到我们的要求了。

select
case when Curricula='语文' then name end 语文,
case when Curricula='数学' then name end 数学,
case when Curricula='英语' then name end 英语,
case when Curricula='马哲' then name end 马哲,
sn
from
(
select curricula,name,mark,
row_number() over(partition by curricula order by name) sn
from stgrade where mark>59
) t

语文 数学 英语 马哲 sn
NULL NULL NULL 城南 1
NULL 李四 NULL NULL 1
王朝 NULL NULL NULL 1
张三 NULL NULL NULL 2

select sn,
max(case when Curricula='语文' then name end) 语文,
max(case when Curricula='数学' then name end) 数学,
max(case when Curricula='英语' then name end) 英语,
max(case when Curricula='马哲' then name end) 马哲
from
(
select curricula,name,mark,
row_number() over(partition by curricula order by name) sn
from stgrade where mark>59
) t
group by sn

sn 语文 数学 英语 马哲
1 王朝 李四 NULL 城南
2 张三 NULL NULL NULL

上一篇:LINQ之路10:LINQ to SQL 和 Entity Framework(下)


下一篇:servlet部分知识总结