ratio_to_report分析函数求占比
2015-12-31 17:24
316 查看
drop table test;
create table test
(
name varchar(20),
kemu varchar(20),
score number
);
insert into test values('testa','yuwen',10);
insert into test values('testa','英语',100);
insert into test values('testb','yuwen',60);
insert into test values('testb','yuwen',120);
insert into test values('testc','yuwen',40);
select name,
score,
ratio_to_report(score) over() as "占所有科目的百分比",
ratio_to_report(score) over(partition by kemu) as "占各科目的百分比"
from test ;
NAME SCORE 占所有科目的百分比 占各科目的百分比
-------------------- ---------- ------------------ ----------------
testa 10 .03030303 .043478261
testb 60 .181818182 .260869565
testc 40 .121212121 .173913043
testb 120 .363636364 .52173913
testa 100 .303030303 1
drop table test;
试想下假设我们没有这个分析函数,实现就有可能如下:
select name,score,
(score/sum(score) over()) as "占所有科目的百分比",
(score/sum(score) over(partition by kemu)) as "占所有科目的百分比"
from test
group by name,score,kemu
order by 2;
嘿嘿,还是没有那个方便,估计效率也不咋的。
总结:1. 有了ratio_to_report分析函数,我们避免了还需要写分析函数,自己相除的写法,SQL简单实现了。
2. site:download.oracle.com ratio_to_report 搜索oracle官方文档
create table test
(
name varchar(20),
kemu varchar(20),
score number
);
insert into test values('testa','yuwen',10);
insert into test values('testa','英语',100);
insert into test values('testb','yuwen',60);
insert into test values('testb','yuwen',120);
insert into test values('testc','yuwen',40);
select name,
score,
ratio_to_report(score) over() as "占所有科目的百分比",
ratio_to_report(score) over(partition by kemu) as "占各科目的百分比"
from test ;
NAME SCORE 占所有科目的百分比 占各科目的百分比
-------------------- ---------- ------------------ ----------------
testa 10 .03030303 .043478261
testb 60 .181818182 .260869565
testc 40 .121212121 .173913043
testb 120 .363636364 .52173913
testa 100 .303030303 1
drop table test;
试想下假设我们没有这个分析函数,实现就有可能如下:
select name,score,
(score/sum(score) over()) as "占所有科目的百分比",
(score/sum(score) over(partition by kemu)) as "占所有科目的百分比"
from test
group by name,score,kemu
order by 2;
嘿嘿,还是没有那个方便,估计效率也不咋的。
总结:1. 有了ratio_to_report分析函数,我们避免了还需要写分析函数,自己相除的写法,SQL简单实现了。
2. site:download.oracle.com ratio_to_report 搜索oracle官方文档
相关文章推荐
- linux下启动、关闭tomcat,linux设置tomcat开机自启动
- Mac系统 与 Xcode快捷键整理
- C++ unordered_map自定义KEY用法以及insert无法覆盖解决办法
- oracle 游标实例
- mybatis与ibatis区别
- close form 封闭解
- android瀑布流效果(仿蘑菇街)
- QR二维码Java开发笔记
- excel表格中的数据如何放到数据库中(非导入)
- 测序数据质量控制
- 老李推荐:第8章5节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行测试脚本
- 约瑟夫问题-数组实现
- phonegap(cordova) 自定义插件代码篇(二)----android 自动更新
- android_自动检测更新
- Global Azure SQL Server Database 导出和导入配置介绍
- Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJE
- 命令行下构建xcode工程(build和archive)
- Activity launchMode四分之二 ----singleTop
- diskpart删除保护分区的方法
- mysql concat乱码