oracle行转列sql

建成绩表gradeinfo和学生表student,成绩表中存有学生表的id:

create table GRADEINFO
(
  SID     VARCHAR2(2),
  SUBJECT VARCHAR2(20),
  GRADE   VARCHAR2(20)
)
create table STUDENT
(
  ID   VARCHAR2(2),
  NAME VARCHAR2(20)
)

 要查询出每一个学生对应的每一门功课的分数是多少。

 想要的效果如下:


查询SQL:

select a.name,sum(decode(b.subject,'语文', b.grade)) "语文",
sum(decode(b.subject,'数学', b.grade)) "数学",
sum(decode(b.subject,'英语', b.grade)) "英语"
from student a,gradeinfo b where b.sid=a.id
group by a.name

猜你喜欢

转载自geeksun.iteye.com/blog/1575410