----------------------第一题---------------------------
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. iOS耗电量测试方法及其数据收集

    常用的电量测试方法: 硬件测试(硬件要求比较高,成本比较大,这里介绍软件测试方法) 软件工具检测 几个典型的耗电场景如下: 定位,尤其是调用GPS定位 网络传输,尤其是非Wifi环境 cpu频率 内存 ...

  2. poj2114树分治

    题意:给你一棵树,每条边有权值,求有没有一条链使得权值和为k 题解:和上一题类似,依旧是树分治,只是我们储存结果的时候是判断加起来为k的点对数,刚开始本来想用map存答案,结果就t了,后来用了vect ...

  3. poj 2513 欧拉图/trie

    http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submi ...

  4. 浅谈 C# CLR 执行模块

    前言: 买了这本 CLR via C# 已有些日子了,但是一直没有认真翻过这本书,以前学的知识点都忘光了. 趁着现在下着雨的周末,大体记录一下今天了解的笔记,也好弥补一下 C# 知识上的一些盲点.   ...

  5. spring mvc中,如何在 Java 代码里,获取 国际化 内容

    首先,在Spring的application.xml中定义 <bean id="messageSource" class="org.springframework. ...

  6. Python学习之路day4-列表生成式、生成器、Iterable和Iterator

    一.列表生成式 顾名思义,列表生成式就是用于生成列表的特殊语法形式的表达式. 1.1 语法格式 [exp for iter_var in iterable] 工作过程: 1.通过iter_var迭代i ...

  7. 26 python 并发编程之多进程理论

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  8. 在Windows 7上安装ACE 6.1.0

    主机环境    操作系统:Windows 7 专业版准备ACE    用浏览器打开http://download.dre.vanderbilt.edu/,下载ACE-6.1.0和ACE-html-6. ...

  9. 242. Valid Anagram Add to List

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  10. IntelliJ IDEA 12创建Maven管理的Java Web项目(图解)

    转:http://blog.csdn.net/zht666/article/details/8673609/ 本文主要使用图解介绍了使用IntelliJIDEA 12创建Maven管理的JavaWeb ...