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 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...
随机推荐
- Eclipse插件开发_学习_00_资源帖
一.官方资料 1.eclipse api 2.GEF Developer's Guide 二. 精选资料 1.开发 Eclipse 插件 2.Eclipse, RCP, Plugin and OSGi ...
- Sunday算法
Sunday算法是Daniel M.Sunday于1990年提出的字符串模式匹配.其核心思想是:在匹配过程中,模式串发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率 ...
- hdoj-1276-士兵队列训练问题(队列模拟)
题意: 新兵蛋子按照两种报数规则报数: 1.1212报数,2出队 2.123123报数,3出队 没报完一轮,检查人数,不大于3,over 略坑,必须每报完一轮检查人数,最初,按照12两种顺序报完检查人 ...
- 3.18 CCProgressTo 进度计时器
CCProgressTimer * pross = CCProgressTimer::create(CCSprite::create("Icon.png")); pross-> ...
- PostgreSQL学习手册 性能提升技巧
http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 一.使用EXPLAIN: PostgreSQL为每个查询都生成一个查询 ...
- Cash Machine(多重背包二进制转换)
个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...
- 爬虫利器 Puppeteer
http://wintersmilesb101.online/2017/03/24/use-phantomjs-dynamic/ 一起学爬虫 Node.js 爬虫篇(三)使用 PhantomJS ...
- YARN的ACL
修改完了资源池的权限之后,发现无法查看日志了.报错: User [dr.who] is not authorized to view the logs for... 即使把资源池的权限设置为了*(所有 ...
- zabbix3.2.1安装graphtrees插件
https://blog.csdn.net/liang_baikai/article/details/53542317 graphtree介绍 由于zabbix的图像显示一块不太友好,图像没法集中显示 ...
- Angular5学习笔记 - 路由管理(五)
一.添加路由管理引用 打开src/app/app.module.ts文件 import {RouterModule} from '@angular/router'; import {Routes} f ...