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官方文档

ratio_to_report 分析函数求占比的更多相关文章

  1. ratio_to_report分析函数求占比

    drop table test; create table test ( name varchar(20), kemu varchar(20), score number  ); insert int ...

  2. nulls last ratio_to_report(id) over() 占比函数

     ORDER BY t3.pctl DESC NULLS LAST http://blog.itpub.net/9932141/viewspace-600751/ http://blog.csdn.n ...

  3. oracle 分析函数

    认识分析函数 分析函数是什么? 分析函数是oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计值. 分析函数 ...

  4. SQL优化一

    1.行列转换: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值); select decode(sign(变量1-变量2),-1,变量1,变量2) from dual ...

  5. SQL优化一(SQL使用技巧)

    1.行列转换: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值); select decode(sign(变量1-变量2),-1,变量1,变量2) from dual ...

  6. 瘋子C++笔记

    瘋耔C++笔记 欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 参考:C++程序设计(谭浩强) 参考:http://c.biancheng.net/cpp/biancheng ...

  7. C++笔试题库-------Coding整理

    1. 反转字符串 char* strrev1(const char* str) { int len = strlen(str); ]; char *p = temp + len; *p = '\0'; ...

  8. Flex中宽度计算

    flex 有三个属性值,分别是 flex-grow, flex-shrink, flex-basis,默认值是 0 1 auto. 发现网上详细介绍他们的文章比较少, 今天就详细说说他们,先一个一个看 ...

  9. 弹性盒子模型属性之flex-shrink

    上一次,我们已经了解过flex-grow的具体用法后,这周,让我们一起来见一下flex-basis这个属性. flex-shrink 定义项目的缩小比例,默认值为1,注意前提是空间不足的情况下,项目缩 ...

随机推荐

  1. ip+掩码

    pattern="/^(((?:(?:1[0-9][0-9]\.)|(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)) ...

  2. db2 reorg runstats rebind具体操作

    db2 reorg runstats rebind具体操作 #reorg table db2 -x "select 'reorg table '||rtrim(tabschema)||'.' ...

  3. list utilities监视数据库前滚操作

    您可以使用 db2pd 或 LIST UTILITIES 命令来监视数据库前滚操作的进度. 过程 发出 LIST UTILITIES 命令并指定 SHOW DETAIL 参数 db2 LIST UTI ...

  4. 使用sshfs将远程目录挂载到本地

    使用sshfs将远程目录挂载到本地 转自:http://blog.sina.com.cn/s/blog_6561ca8c0102vc2u.html 在Linux下我们通常使用ssh命令来登录远程Lin ...

  5. Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br

    /** * Created with JetBrains PhpStorm. * User: scotty * Date: 28/08/2013 * Time: 19:39 */ ;(function ...

  6. shell编程学习笔记(三):Shell中局部变量的使用

    现在我们看一下Shell中局部变量的使用 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script03.sh 开始编写script0 ...

  7. DroneCI启用privileged

    https://www.aliyun.com/jiaocheng/123155.html?spm=5176.100033.2.5.EIV4p6 drone的服务需要配置DRONE_ADMIN环境变量, ...

  8. 基于Clang的缓存型C++编译器Zapcc

    http://www.infoq.com/cn/news/2018/06/zapcc-caching-cpp-compiler-open https://blog.csdn.net/joy0921/a ...

  9. NOIP2011普及组 瑞士轮

    OJ地址: https://www.luogu.org/problemnew/show/P1309 http://bailian.openjudge.cn/practice/4031/ 总时间限制:  ...

  10. 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境

    在Linux下没有像keli那样好用的IDE来开发51单片机,开发环境只能自己搭建了. 第一步:安装交叉编译工具 a) 安装SDCC sudo apt-get install sdcc b)测试SDC ...