Oracle SQL - Analytic Functions CUME_DIST

Introduction

The syntax for the CUME_DIST function:

CUME_DIST() OVER ([PARTITION clause] ORDER clause) 

The PARTITION clause is optional.

The CUME_RANK function calculates the cumulative distribution in a group of values.

In our example, we have only one group, so the formula works like this:

Cumulative Distribution = the highest rank for that row (cr)/number of rows (nr)

SELECT cname, temp, 
  RANK() OVER(ORDER BY temp) RANK, 
  PERCENT_RANK() OVER(ORDER BY temp) PR, 
  CUME_DIST() OVER(ORDER BY temp) CD 
FROM cities 
ORDER BY temp;

Demo

SQL>
SQL> drop table emp;

Table dropped.-- from  w ww.  j  av  a2s  .c  o m

SQL> create table emp(
  2  empno        Number(3)  NOT NULL,
  3  ename        VARCHAR2(10 BYTE),
  4  hireDate     DATE,
  5  orig_salary  Number(8,2),
  6  curr_salary  Number(8,2),
  7  region       VARCHAR2(1 BYTE)
  8  );
SQL>
SQL> insert into emp values(1,'Alison',DATE '1996-03-21', 45000, NULL,  'E');
SQL> insert into emp values(2,'James',DATE  '1978-12-12', 23000, 32000, 'W');
SQL> insert into emp values(3,'Celia',DATE  '1982-10-24', NULL,  58000, 'E');
SQL> insert into emp values(4,'Robert',DATE '1984-01-15', 31000, NULL,  'W');
SQL> insert into emp values(5,'Linda',DATE  '1987-07-30', NULL,  53000, 'E');
SQL> insert into emp values(6,'David',DATE  '1990-12-31', 78000, NULL,  'W');
SQL> insert into emp values(7,'Jode',DATE  '1996-09-17', 21000, 29000, 'E');
SQL>
SQL> SELECT ename, orig_salary,
  2    RANK() OVER(ORDER BY orig_salary) RANK,
  3    PERCENT_RANK() OVER(ORDER BY orig_salary) PR,
  4    CUME_DIST() OVER(ORDER BY orig_salary) CD
  5  FROM emp
  6  ORDER BY orig_salary;

ENAME      ORIG_SALARY       RANK         PR         CD
---------- ----------- ---------- ---------- ----------
Jode             21000          1          0 .142857143
James            23000          2 .166666667 .285714286
Robert           31000          3 .333333333 .428571429
Alison           45000          4         .5 .571428571
David            78000          5 .666666667 .714285714
Linda                           6 .833333333          1
Celia                           6 .833333333          1

7 rows selected.

SQL>