ITPub博客

首页 > 数据库 > Oracle > oracle 多行转多列查询

oracle 多行转多列查询

原创 Oracle 作者:bizhanjie 时间:2014-11-25 16:52:34 0 删除 编辑
测试成绩表t

create table t(
               stuid varchar2(20),
               course varchar2(20),
               score number(8,2));

insert into t values('1','math',80);  
insert into t values('1','chinese',90);
insert into t values('1','english',85);
insert into t values('1','history',78);
insert into t values('2','math',82);  
insert into t values('2','chinese',86); 
insert into t values('2','english',90);


15:51:49 BZJ@itps>select * from t;
STUID                COURSE                    SCORE
-------------------- -------------------- ----------
1                    math                         80
1                    chinese                      90
1                    english                      85
1                    history                      78
2                    math                         82
2                    chinese                      86
2                    english                      90
2                    history                      88

8 rows selected.

比如t表记录每名学生本学年所有考试的成绩(course列是学科,改为考试名称),那么我们想查询到每名学生考试的前三得分,并用一列表示,则

16:07:12 BZJ@itps>select stuid,max(decode(rn,1,score,0)) score_1,
16:07:27   2  max(decode(rn,2,score,0)) score_2,
16:07:33   3  max(decode(rn,3,score,0)) score_3
16:07:36   4  from (select stuid,score,row_number() over(partition by stuid order by score desc) rn from t)                        -------------此处可以采用(rank() over,dense_rank(),row_number())其中之一,三个函数有区别,在此不作说明。
16:07:48   5  group by stuid;

STUID                   SCORE_1    SCORE_2    SCORE_3
-------------------- ---------- ---------- ----------
1                            90         85         80
2                            90         88         86

现在回归到表t记录学生一次考试每门学科的成绩,我们想要用一列展示一名学生的所有学科的成绩,则

方法一:
16:13:31 BZJ@itps>select stuid,
16:13:37   2  max(decode(course,'math',score,0)) math,
16:13:54   3  max(decode(course,'chinese',score,0)) chinese,
16:14:08   4  max(decode(course,'english',score,0)) english,
16:14:24   5  max(decode(course,'history',score,0)) history
16:14:37   6  from t group by stuid;

STUID                      MATH    CHINESE    ENGLISH    HISTORY
-------------------- ---------- ---------- ---------- ----------
1                            80         90         85         78
2                            82         86         90         88

Elapsed: 00:00:00.01

方法二:
16:14:46 BZJ@itps>select stuid,
16:18:29   2  max(case course when 'math' then score else 0 end) math,
16:19:48   3  max(case course when 'chinese' then score else 0 end) chinese,
16:20:18   4  max(case course when 'english' then score else 0 end) english,
16:20:48   5  max(case course when 'history' then score else 0 end) history
16:21:17   6  from t group by stuid;

STUID                      MATH    CHINESE    ENGLISH    HISTORY
-------------------- ---------- ---------- ---------- ----------
1                            80         90         85         78
2                            82         86         90         88

Elapsed: 00:00:00.00

我们也可以直接得出改学生本次考试总分,则

16:53:30 BZJ@itps>select stuid,
16:53:48   2  max(decode(course,'math',score,0)) math,
16:53:51   3  max(decode(course,'chinese',score,0)) chinese,
16:53:54   4  max(decode(course,'english',score,0)) english,
16:53:56   5   max(decode(course,'history',score,0)) history,
16:54:00   6  sum(score) total
16:54:09   7  from t group by stuid;

STUID                      MATH    CHINESE    ENGLISH    HISTORY      TOTAL
-------------------- ---------- ---------- ---------- ---------- ----------
1                            80         90         85         78        333
2                            82         86         90         88        346

Elapsed: 00:00:00.00

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29193965/viewspace-1346248/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: SQL TUNING ADVISOR
请登录后发表评论 登录
全部评论

注册时间:2014-09-18

  • 博文量
    41
  • 访问量
    296968