【发布时间】:2017-10-24 06:20:08
【问题描述】:
我有一个表格输出如下:
class | student | week | eng | maths | science
1 | stu1 | 1 | 67 | 78 | 89
1 | stu2 | 1 | 78 | 88 | 90
1 | stu3 | 1 | 45 | 34 | 45
1 | stu1 | 2 | 67 | 45 | 34
我需要最终输出为:
class | student | sub | week1 | week2
1 | stu1 | eng | 67 | 67
1 | stu1 | maths | 78 | 45
1 | stu1 | science | 89 | 34
1 | stu2 | eng | 78 | 89
1 | stu2 | maths | 88 | 90
1 | stu2 | science | 90 | 89
1 | stu3 | eng | 45 |
1 | stu3 | maths | 34 |
1 | stu3 | science | 45 |
即在表格中,仅对一部分,行转列,列转行。
我正在使用 oracle 10g。
这是我对第一个表的查询。
select class, student,
to_char(dt,'WW') as week,
sum(case when sm.sub='eng' then 'eng' end) as eng,
sum(case when sm.sub='maths' then 'maths' end) as maths,
sum(case when sm.sub='science' then 'science' end) as science
from studentList sl left join studentMarks sm
on sl.stId=sm.stId
group by class, student, to_char(dt,'WW')
order by class, student;
【问题讨论】: