----------------------第一题---------------------------
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. dyci——IOS动态代码注入

    有时候用xib,更改了布局需要重新运行才可以看到效果,对于比较复杂的应用尤其浪费时间,下面介绍一个工具dyci-不需要重Run应用,也能看到效果 yci的网址:https://github.com/D ...

  2. C++中string的常见用法

    在ACM中主要用到string的这几个功能:赋值,添加,删除,替换,查找,比较,反向排序. 1.赋值 直接来就行: string ss; ss="aaa"; 或者 string s ...

  3. Codeforces Round #253 (Div. 2)B(暴力枚举)

    就暴力枚举所有起点和终点就行了. 我做这题时想的太多了,最简单的暴力枚举起始点却没想到...应该先想最简单的方法,层层深入. #include<iostream> #include< ...

  4. VueJs路由跳转——vue-router的使用

    对于单页应用,官方提供了vue-router进行路由跳转的处理,本篇主要也是基于其官方文档写作而成. 安装 基于传统,我更喜欢采用npm包的形式进行安装. npm install vue-router ...

  5. UVA - 1610 Party Games (字符串比较)

    给你n(n为偶数)个字符串,让你找出一个长度最短且字典序尽量小的字符串,使得一半的字符串小于等于该串,一半的字符串大于该串. 紫薯上说这道题有坑,但其实思路对了就没什么坑. 很明显,只要取夹在中间两个 ...

  6. GC(Garbage Collection)垃圾回收机制

    1.在垃圾回收器中,程序员没有执行权,只有通知它的权利. 2.程序员可以通过System.gc().通知GC运行,但是Java规范并不能保证立刻运行. 3.finalize()方法,是java提供给程 ...

  7. 19年博客flag

    目录 为什么没有年终总结 为什么今天更新了 19年博客flag 个人博客链接:我在马路边 https://hhongwen.cn 更好的阅读体验点击查看原文:19年博客flag 原创博客,转载请注明出 ...

  8. Brackets Sequence(升级版)

    个人心得:又是途径问题,我怕是又炸了.看了题解他的意思就是找出最短的添加顺序的断点,则只要 根据断点添加就好了,注意递归的奥妙之处吧,暂时还真得是拿他没办法. 题目描述: 定义合法的括号序列如下: 1 ...

  9. 【LeetCode】006. ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. java 连接oracle数据库

    package shujuku; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...