oracle学生成绩行转列

1、第一步先创造几条学生成绩数据

drop table chengji;
create table chengji (
  stu_id         varchar2(30) ,
  kemu       		varchar2(30),
  chengji        varchar2(50)
);
insert into chengji(stu_id, kemu, chengji) values ('student1','语文',123);
insert into chengji(stu_id, kemu, chengji) values ('student1','数学',125);
insert into chengji(stu_id, kemu, chengji) values ('student1','英语',128);
insert into chengji(stu_id, kemu, chengji) values ('student2','语文',110);
insert into chengji(stu_id, kemu, chengji) values ('student2','数学',120);
insert into chengji(stu_id, kemu, chengji) values ('student2','英语',130);
insert into chengji(stu_id, kemu, chengji) values ('student3','语文',129);
insert into chengji(stu_id, kemu, chengji) values ('student3','数学',126);
insert into chengji(stu_id, kemu, chengji) values ('student3','英语',125);
commit;

2、查看chengji表数据

select * from chengji;

在这里插入图片描述
3、行转列(用sum(case when))

select stu_id
,sum(case when kemu='数学' then chengji else '0' end) as "数学"
,sum(case when kemu ='语文' then chengji  else '0' end) as "语文"
,sum(case when kemu ='英语' then chengji else '0' end) as "英语"
from chengji
group by stu_id;

结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值