ORACLE实用函数之一 ratio_to_report的简单使用

应用场景: 查询学生成绩级别(ABCDE)个人数和所占百分比(案列简单,勿喷)。 表结构:

create or replace table stu_grade(
    id varchar2(36),  
    level varchar(1) 
);

取各级别人数:

select level,count(id) num from stu_grade group by level;

取总人数:

select sum(1) from stu_grade;

这个是大家第一反应想到的sql,可以查询出相应级别的人数和总的人数。但是我们同时需要这两个sql的结果。 这个时候,就是使用ratio_to_report的时候了

select level,count(id) num,ratio_to_report(id) over() present from stu_grade group by level;

系列博客:ORACLE 实用函数使用案列

原文地址:https://www.cnblogs.com/Springmoon-venn/p/6709212.html