您好,欢迎访问一九零五行业门户网

Oracle—decode函数行转列

文章说明:根据成绩表,对每个学生的成绩数据进项行转列操作,最后每行显示每个学生的各科成绩。 以下是行转列的各个步骤操作。 1、创建学生信息,课程,成绩三张表 create table tb_student( studentid varchar2(10) primary key, stuname varchar2(50), se
文章说明:根据成绩表,对每个学生的成绩数据进项行转列操作,最后每行显示每个学生的各科成绩。
以下是行转列的各个步骤操作。
1、创建学生信息,课程,成绩三张表
create table tb_student( studentid varchar2(10) primary key, stuname varchar2(50), sex varchar2(8), age number(5) );create table tb_course( courseid varchar2(10) primary key, coursename varchar2(10), teacher varchar2(10));create table tb_score( scoreid varchar2(10) primary key, courseid varchar2(10), studentid varchar2(10), grade number(10));
2、给三张表插入相应的数据insert into tb_student(studentid,stuname,sex,age) values('20141101','sim','boy',22);insert into tb_student(studentid,stuname,sex,age) values('20141102','mary','girl',22);insert into tb_student(studentid,stuname,sex,age) values('20141103','silly','girl',22);insert into tb_student(studentid,stuname,sex,age) values('20141104','alice','girl',22);insert into tb_student(studentid,stuname,sex,age) values('20141105','sam','boy',22);insert into tb_course(courseid,coursename,teacher) values('1001','math','sa');insert into tb_course(courseid,coursename,teacher) values('1002','english','alice');insert into tb_course(courseid,coursename,teacher) values('1003','java','bb');insert into tb_score(scoreid,courseid,studentid,grade) values('10001','1001','20141101',99);insert into tb_score(scoreid,courseid,studentid,grade) values('10002','1001','20141102',88);insert into tb_score(scoreid,courseid,studentid,grade) values('10003','1001','20141103',77);insert into tb_score(scoreid,courseid,studentid,grade) values('10004','1001','20141104',98);insert into tb_score(scoreid,courseid,studentid,grade) values('10005','1001','20141105',96);insert into tb_score(scoreid,courseid,studentid,grade) values('10006','1002','20141101',99);insert into tb_score(scoreid,courseid,studentid,grade) values('10007','1002','20141103',99);insert into tb_score(scoreid,courseid,studentid,grade) values('10008','1002','20141104',99);insert into tb_score(scoreid,courseid,studentid,grade) values('10009','1003','20141102',99);insert into tb_score(scoreid,courseid,studentid,grade) values('10010','1003','20141105',99);
插入后的数据结果如下:
studentid stuname sex age---------- -------------------------------------------------- -------- ------20141101 sim boy 2220141102 mary girl 2220141103 silly girl 2220141104 alice girl 2220141105 sam boy 22courseid coursename teacher---------- ---------- ----------1001 math sa1002 english alice1003 java bbscoreid courseid studentid grade---------- ---------- ---------- -----------10001 1001 20041101 9910002 1001 20041102 8810003 1001 20041103 7710004 1001 20041104 9810005 1001 20041105 9610006 1002 20041101 9910007 1002 20041103 9910008 1002 20041104 9910009 1003 20041102 9910010 1003 20041105 99
3、对数据进行行转列操作select g.studentid 学号,(select stu.stuname from tb_student stu where stu.studentid=g.studentid) as 姓名,sum(decode(g.courseid,'1001',grade,0)) 数学,sum(decode(g.courseid,'1002',grade,0)) 英语,sum(decode(g.courseid,'1003',grade,0)) javafrom tb_score ggroup by g.studentidorder by g.studentid;
说明:decode函数除了可以进行数据匹配以外,还可以进行行转列的功能decode函数格式:decode(column,if1,then1,defalut)
表示列如果满足if1的条件,就显示then1的内容,不满足就显示default的值!
sum(decode(...))是对decode返回的数据进行累加
4、通过上面的查询,最后显示的查询结果如下
学号 姓名 数学 英语 java---------- -------------------------------------------------- ---------- ---------- ----------20141101 sim 99 99 020141102 mary 88 0 9920141103 silly 77 99 020141104 alice 98 99 020141105 sam 96 0 99
以上是通过oracle的decode函数进行的行转列操作,初次写博文,多多指教!
其它类似信息

推荐信息