竞争无处不在,青春永不言败!专业撸代码,副业修bug

Talk is cheap , show me the code!



论坛上的题目--类似于行列转换的,给出我的方法。

论坛上的题目:

现在有表如下
学生姓名 选课科目
张三 语文
李四 英语
张三 数学
王五 政治
张三 体育
张三 美术
李四 语文
张三 英语

要求得如下结果
学生姓名              选了语文数学英语的科目数量               选了所有课程的科目数量
张三                                              3                                                              5
李四                                              2                                                              2
王五                                              0                                                              1

个人觉得,类似于行列转换的写法

;with ta(stu_name,stu_subject)  as

(
select 'Jim','Chinese'
union
select 'Tom','English'
union
select 'Jim','Math'
union 
select 'Anna','political'
union
select 'Jim','PE'
union 
select 'Jim','Art'
union 
select 'Tom','Chinese'
union
select 'Jim','English'
)
select stu_name,sum(case when stu_subject 
in('Chinese','English','Math') then 1 else 0 end ) as countofenglishchinesemath,
sum(case when stu_subject is not null then 1 else 0 end) as allsubjects 
from ta group by stu_name

posted @ 2014-12-12 06:46  云雾散人  阅读(153)  评论(0编辑  收藏  举报

Your attitude not your aptitude will determine your altitude!

如果有来生,一个人去远行,看不同的风景,感受生命的活力!