排列百分比 PERCENT_RANK
例 求图书的现价排列百分比。
SELECT NAME, PERCENT_RANK() OVER(ORDER BY NOWPRICE) AS NTILE FROM
PRODUCTION.PRODUCT;
查询结果如下:
NAME | NTILE |
---|---|
老人与海 | 0.000000000000000E+000 |
突破英文基础词汇 | 1.111111111111111E-001 |
工作中无小事 | 2.222222222222222E-001 |
水浒传 | 3.333333333333333E-001 |
红楼梦 | 4.444444444444444E-001 |
连续百分比 PERCENTILE_CONT 和分布百分比 PERCENTILE_DISC 分析函数。
PERCENTILE_CONT函数也是Oracle中用于计算分位数的窗口函数之一。与PERCENTILE_DISC函数不同的是,PERCENTILE_CONT函数会返回一个连续值,而不是精确匹配给定百分位的数据值。
<PERCENTILE_CONT|PERCENTILE_DISC> (< 参 数 >) WITHIN GROUP(<ORDER BY 项 >) OVER
([<PARTITION BY 项>])
SELECT NAME, PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY NOWPRICE) OVER() AS
PERCENTILE_CONT FROM PRODUCTION.PRODUCT;
查询结果如下:
NAME | PERCENTILE_CONT |
---|---|
老人与海 | 17.6 |
突破英文基础词汇 | 17.6 |
工作中无小事 | 17.6 |
水浒传 | 17.6 |
红楼梦 | 17.6 |
鲁迅文集(小说、散文、杂文)全两册 | 17.6 |
使用方法
创建一个临时表
create table EMP
(
EMPNO NUMBER(4) not null,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2)
);
插入数据
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800.00, null, 20);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600.00, 300.00, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250.00, 500.00, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975.00, null, 20);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250.00, 1400.00, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850.00, null, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450.00, null, 10);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000.00, null, 10);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500.00, 0.00, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100.00, null, 20);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950.00, null, 30);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300.00, null, 10);
commit;
查询表:
select * from emp e;
按薪水的倒序,取薪水的排名,及一定百分比的薪水值
select e.ename,e.sal,e.deptno,
percent_rank() over(partition by deptno order by sal desc) p_rank, --以deptno分组,以sal倒序,取出百分比排名
PERCENTILE_CONT(0) within group(order by sal desc)
over(partition by deptno) max_sal , --以deptno分组,以sal倒序,取出占0%的薪水
PERCENTILE_CONT(0.25) within group(order by sal desc)
over(partition by deptno) max_sal_25, --以deptno分组,以sal倒序,取出25%的薪水
PERCENTILE_CONT(0.5) within group(order by sal desc)
over(partition by deptno) max_sal_50, --以deptno分组,以sal倒序,取出50%的薪水
PERCENTILE_CONT(0.75) within group(order by sal desc) --以deptno分组,以sal倒序,取出70%的薪水
over(partition by deptno) max_sal_75
from emp e;