----------------------第一题---------------------------
create table STUDENT_SCORE
(
name VARCHAR2(20),
subject VARCHAR2(20),
score NUMBER(4,1)
) insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '语文', 78.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '数学', 88.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '英语', 98.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '语文', 89.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '数学', 76.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '英语', 90.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '语文', 99.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '数学', 66.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '英语', 91.0); --emp
/*
显示格式:
语文 数学 英语
及格 优秀 不及格
*/
--方法一
select name,
(select score from student_score s1 where subject = '语文' and s1.name=s.name) 语文,
(select score from student_score s1 where subject = '数学' and s1.name=s.name) 数学,
(select score from student_score s1 where subject = '英语' and s1.name=s.name) 英语
from student_score s group by name --方法二 decode
select s.name,
sum(decode(subject, '语文',s.score,0)) 语文,
sum(decode(subject, '数学',s.score,0)) 数学,
sum(decode(subject, '英语',s.score,0)) 英语
from student_score s
group by s.name
--方法三 case when
select s.name , sum(case s.subject when '语文' then s.score else 0 end) "语文",
sum(case s.subject when '数学' then s.score else 0 end) 数学,
sum(case s.subject when '英语' then s.score else 0 end) 英语
from student_score s group by s.name
--方法四
采用 join表连接的方式 --判断及格否
select t.name 名字,
case
when t.y between 90 and 100 then
'优秀'
when t.y between 60 and 90 then
'及格'
when t.y between 0 and 60 then
'不及格'
end 语文,
case
when t.s between 90 and 100 then
'优秀'
when t.s between 60 and 90 then
'及格'
when t.s between 0 and 60 then
'不及格'
end 数学,
case
when t.e between 90 and 100 then
'优秀'
when t.e between 60 and 90 then
'及格'
when t.e between 0 and 60 then
'不及格'
end 英语 from ( select s.name,
sum(decode(subject, '语文', s.score, 0)) y,
sum(decode(subject, '数学', s.score, 0)) s,
sum(decode(subject, '英语', s.score, 0)) e
from student_score s
group by s.name) t -----------------------第二题-------------------------------- create table test(
id number(10) primary key,
type number(10) ,
t_id number(10),
value varchar2(5)
);
insert into test values(100,1,1,'张三');
insert into test values(200,2,1,'男');
insert into test values(300,3,1,'');
/*
姓名 性别 年龄
--------- -------- ----
张三 男 50
*/ --方法一
--
select listagg(decode(t.type, 1, t.value)) within group(order by value) 姓名,
listagg(decode(t.type, 2, t.value)) within group(order by value) 性别,
listagg(decode(t.type, 3, t.value)) within group(order by value) 年龄
from test t
group by t.t_id --方法二
select max(decode(t.type, 1, t.value)) 姓名,
max(decode(t.type, 2, t.value)) 性别,
max(decode(t.type, 3, t.value))年龄
from test t group by t.t_id --方法三表连接方式
select * from test select *
from (select value name,t_id from test where type = 1) m1
join (select value sex,t_id from test where type = 2) m2
on m1.t_id = m2.t_id -------------------------第三题------------------------- create table tmp(rq varchar2(10),shengfu varchar2(5)) insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-10','胜');
insert into tmp values('2005-05-10','负');
insert into tmp values('2005-05-10','负'); select * from tmp;
胜 负
2005-05-09 2 2
2005-05-10 1 2 --方法一
select rq,
sum(decode(shengfu, '胜', 1, '负', 0)) 胜,
sum(decode(shengfu, '胜', 0, '负', 1)) 负
from tmp
group by rq

oracle行转列练习的更多相关文章

  1. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  2. Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

    这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...

  3. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  4. Oracle行转列、列转行的Sql语句总结(转)

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...

  5. oracle 行转列、列转行

    最近做数据处理,经常遇到需要行转列.列转行的场景,记录个非常简单实用的oracle  列转行.行转的列方法 1.行转列,基础数据如下 做行转列处理 处理SQL select user_name,max ...

  6. Oracle行转列操作

    有时候我们在展示表中数据的时候,需要将行转为列来显示,如以下形式: 原表结构展示如下:---------------------------产品名称    销售额     季度------------ ...

  7. oracle行转列(连接字符串函数)

    方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面的参数是列,而且可以是多个列的集 ...

  8. Oracle行转列的函数

    --行转列的函数-- CREATE OR REPLACE FUNCTION Calvin( col IN VARCHAR2,dw IN VARCHAR2) RETURN VARCHAR2 IS ret ...

  9. oracle 行转列 列转行

    行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...

  10. oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...

随机推荐

  1. Android自定义组件之ListView

    1-ListView简介 在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.一个ListView通常有两个职责. (1)将数据填充到 ...

  2. java中base64

    // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (n ...

  3. 06-THREE.JS 给所有物体相同的材质

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  4. 3 Python os 文件和目录

    ile 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...

  5. datatable绑定comboBox,在下拉菜单中显示对应数据

    实现功能: datatable绑定comboBox,在下拉菜单中显示对应数据 实现方法: .生成datatable,并为combox绑定数据源: comboBox1.DataSource = dt1; ...

  6. hdoj-1031-Design T-Shirt

    题目链接 /* 两次排序,搞定 */ #include <cstring> #include <iostream> #include <algorithm> usi ...

  7. 六、python沉淀之路--int str list tuple dict 重点总结

    一.数字int(..)二.字符串replace/find/join/strip/startswith/split/upper/lower/formattempalte = "i am {na ...

  8. mongodb所在目录空间不足解决方法

    1.原理是将目录/home/aa软连接到/usr/lib/下,以后从/usr/lib下读取的内容其实都是放在/home/aa下. 建议不要大范围动/usr下的内容,咋着也是属于系统目录,可能会对已装软 ...

  9. C的随想

    c用的是操作系统函数,这个一下子就限制了APi的数量,通过组合这些系统api即可实现功能. c开发的人一般都会熟记系统函数,然后需要确定函数参数的时候,通过man指令进行查看 对于32位64位将会导致 ...

  10. PKU campus 2018 A Wife——差分约束?/dp

    题目:http://poj.openjudge.cn/campus2018/A 有正规的差分约束做法,用到矩阵转置等等. 但也有简单(?)的dp做法. 有一个结论(?):一定要么在一天一点也不选,要么 ...