MEDIAN CUME_DIST PERCENT_RANK PERCENTILE_CONT PERCENTILE_DISC

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 
Connected as [email protected]:1521/orcl


MEDIAN  中位数


以0到1之间的分数形式返回某个值在数据分区中的排名

CUME_DIST

(0,1]

取不到0

PERCENT_RANK

[0,1]

0各1都可以取到


接收一个0到1之间的几率值

PERCENTILE_CONT

连续分布模型

按percent_rank返回值计算对应值的内插值百分比

PERCENTILE_DISC

离散分布模型

升序时,取比所传递的参数cume_dist值更大的值

降序时,取比所传递的参数cume_dist值更小的值



MEDIAN

SQL> SELECT MEDIAN(SAL) FROM EMP WHERE DEPTNO = 20;
MEDIAN(SAL)
-----------
       2975

PERCENTILE_CONT

SQL> SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY SAL)  FROM EMP WHERE DEPTNO = 20;


PERCENTILE_CONT(0.5)WITHINGROU
------------------------------
                          2975

对比查看CUME_DIST 与 PERCENT_RANK

SELECT '1' AS u,
       emp.ename,
       emp.sal,
       cume_dist() over(ORDER BY sal) AS cume_dist,
       percent_rank() over(ORDER BY sal) AS percent_rank1,
       percent_rank() over(ORDER BY sal DESC) AS percent_rank2
  FROM (SELECT deptno,
               CASE WHEN ename = 'SCOTT' THEN 4000 ELSE sal END AS sal,
               ename
          FROM emp
         WHERE deptno = 20
         ORDER BY sal, ename) emp
 WHERE deptno = 20
UNION ALL
SELECT '2' AS union_1,
       emp.ename,
       emp.sal,
       cume_dist() over(ORDER BY sal) AS cume_dist,
       percent_rank() over(ORDER BY sal) AS percent_rank1,
       percent_rank() over(ORDER BY sal DESC) AS percent_rank2
  FROM emp
 WHERE deptno = 20
 ORDER BY 1, 3, 2;

对比查看 PERCENTILE_CONT PERCENTILE_DISC

CREATE OR REPLACE VIEW v AS 
SELECT deptno,
       CASE
         WHEN ename = 'SCOTT' THEN
          4000
         ELSE
          sal
       END AS sal,
       ename,
       row_number() over(ORDER BY sal, ename) AS seq
  FROM emp
 WHERE deptno = 20;

SELECT 0.2 AS pct,
       percentile_cont(0.2) within GROUP(ORDER BY sal) AS percentile_cont,
       percentile_disc(0.2) within GROUP(ORDER BY sal) AS percentile_disc
  FROM v
 WHERE deptno = 20
UNION ALL
SELECT 0.4 AS pct,
       percentile_cont(0.4) within GROUP(ORDER BY sal) AS percentile_cont,
       percentile_disc(0.4) within GROUP(ORDER BY sal) AS percentile_disc
  FROM v
 WHERE deptno = 20
UNION ALL
SELECT 0.45 AS pct,
       percentile_cont(0.45) within GROUP(ORDER BY sal) AS percentile_cont,
       percentile_disc(0.45) within GROUP(ORDER BY sal) AS percentile_disc
  FROM v
 WHERE deptno = 20
UNION ALL
SELECT 0.5 AS pct,
       percentile_cont(0.5) within GROUP(ORDER BY sal) AS percentile_cont,
       percentile_disc(0.5) within GROUP(ORDER BY sal) AS percentile_disc
  FROM v
 WHERE deptno = 20
UNION ALL
SELECT 0.55 AS pct,
       percentile_cont(0.55) within GROUP(ORDER BY sal) AS percentile_cont,
       percentile_disc(0.55) within GROUP(ORDER BY sal) AS percentile_disc
  FROM v
 WHERE deptno = 20

为了便于分析我们把上面的数据放在一起查看

 

SELECT (0.4 - 0.25) / (0.5 - 0.25) * (2975 - 1100) + 1100 from dual

ename

sal

cume_dist

percent_rank asc

percent_rank desc

 

PERCENTILE_CONT

PERCENTILE_DISC

SMITH

800

0.2

0

1

0.2

1040

800

ADAMS

1100

0.4

0.25

0.75

0.4

2225

1100

JONES

2975

0.6

0.5

0.5

0.45

2600

2975

FORD

3000

0.8

0.75

0.25

0.5

2975

2975

SCOTT

4000

1

1

0

0.55

2980

2975

 

 

 

 

 

 

 

 

SMITH

800

0.2

0

1

 

 

 

ADAMS

1100

0.4

0.25

0.75

 

 

 

JONES

2975

0.6

0.5

0.5

 

 

 

FORD

3000

1

0.75

0

 

 

 

SCOTT

3000

1

0.75

0

 

 

注意上面的PERCENTILE_CONT当参数为0.4时,因percent_rank 无对应的值。所以是按内插百分比取值。

计算方法为 SELECT (0.4 - 0.25) / (0.5 - 0.25) * (2975 - 1100) + 1100 from dual


而PERCENTILE_DISC取值在0.45 0.5 0.55时因cume_dist没有对应的值。所以取的是0.6对应的值2975


CSDN表格居然会换行。大家可以复制到word里再看。

你可能感兴趣的:(MEDIAN CUME_DIST PERCENT_RANK PERCENTILE_CONT PERCENTILE_DISC)