oracle decode 函数实现行转列
发布时间:2020-05-23 06:53:02 所属栏目:MySql 来源:互联网
导读:oracle decode 函数实现行转列
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 ----创建测试表
create table student_score(
name varchar2(20),subject varchar2(20),score number(4,1)
);
-----插入测试数据
insert into student_score (name,subject,score)values('张三','语文',78);
insert into student_score (name,'数学',88);
insert into student_score (name,'英语',98);
insert into student_score (name,score)values('李四',89);
insert into student_score (name,76);
insert into student_score (name,90);
insert into student_score (name,score)values('王五',99);
insert into student_score (name,66);
insert into student_score (name,91);
-----decode行转列
select name "姓名",sum(decode(subject,nvl(score,0),0)) "语文",0)) "数学",0)) "英语"
from student_score
group by name;
------ case when 行转列
select name "姓名",sum(case when subject='语文'
then nvl(score,0)
else 0
end) "语文",sum(case when subject='数学'
then nvl(score,0)
else 0
end) "数学",sum(case when subject='英语'
then nvl(score,0)
else 0
end) "英语"
from student_score
group by name;
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
