應用場景: 查詢學生成績級別(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 實用函數使用案列
