oracle行转列练习
----------------------第一题---------------------------
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行转列练习的更多相关文章
- oracle 行转列 分析函数
oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
- Oracle行转列、列转行的Sql语句总结(转)
多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...
- oracle 行转列、列转行
最近做数据处理,经常遇到需要行转列.列转行的场景,记录个非常简单实用的oracle 列转行.行转的列方法 1.行转列,基础数据如下 做行转列处理 处理SQL select user_name,max ...
- Oracle行转列操作
有时候我们在展示表中数据的时候,需要将行转为列来显示,如以下形式: 原表结构展示如下:---------------------------产品名称 销售额 季度------------ ...
- oracle行转列(连接字符串函数)
方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面的参数是列,而且可以是多个列的集 ...
- Oracle行转列的函数
--行转列的函数-- CREATE OR REPLACE FUNCTION Calvin( col IN VARCHAR2,dw IN VARCHAR2) RETURN VARCHAR2 IS ret ...
- oracle 行转列 列转行
行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...
- oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式
转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...
随机推荐
- 《Advanced Bash-scripting Guide》学习(十一):shift的用法
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 Example 4-7 使用shift #!/bin/bash #使用shift ...
- Mac的搜狗输入法和QQ输入法加入⌘⌥⌃⇧自定义短语
搜狗输入法(Mac):http://pinyin.sogou.com/mac/ 创建名为『搜狗输入法自定义短语.ini』的文本文件(建议用Sublime Text),内容如下,然后偏好设置的自定义短语 ...
- MySQL分片 --转自Peter Zaitsev对MySQL分片的建议
本文作者Peter Zaitsev是知名数据库专家,2006年联合创立了Percona.负责维护网站“MySQL性能”.同时,他也是<高性能MySQL>一书的联合作者.以下是他对MySQL ...
- 26 python 并发编程之多进程理论
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- UVALive 3635 Pie(二分法)
简单的二分法应用,循环1000次精度就满足要求了. #include<iostream> #include<cstdio> #include<cstdlib> #i ...
- PHP Smarty无法解析模板文件
/****************************************************************************** * PHP Smarty无法解析模板文件 ...
- Java演示手机发送短信验证码功能实现
我们这里采用阿里大于的短信API 第一步:登陆阿里大于,下载阿里大于的SDK a.在阿里大于上创建自己的应用 b.点击配置管理中的验证码,先添加签名,再配置短信模板 第二步:解压相关SDK,第一个为j ...
- N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...
- TP中登录验证
loginpro 1.建立控制器 loginController.calss.php <?php namespace Admin\Controller; header('Content-type ...
- C#获取路由器外网IP,MAC地址
C#实现的获取路由器MAC地址,路由器外网地址.对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码.至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密 ...