oracle 多行转多列查询

测试成绩表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博客 ” ,链接:http://blog.itpub.net/29193965/viewspace-1346248/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29193965/viewspace-1346248/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值