Oracle Analytic function Percentile_cont

Source: Internet
Author: User

Check the salary of each department in 25%, 50%, 75% position,Percent_rank () is the relative position in the ranking .

CREATE TABLE EMP
(
EMPNO Number (4) is 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

Sql> Select E.ename,e.sal,e.deptno,

Percent_rank () over (partition by deptno ORDER BY Sal Desc) P_rank,
Percentile_cont (0) within group (order by Sal Desc)
Over (partition by Deptno) Max_sal,
Percentile_cont (0.25) within group (order by Sal Desc)
Over (partition by Deptno) max_sal_25,
Percentile_cont (0.5) within group (order by Sal Desc)
Over (partition by Deptno) Max_sal_50,
Percentile_cont (0.75) within group (order by Sal Desc)
Over (partition by Deptno) max_sal_75
From EMP E;
ename SAL DEPTNO p_rank max_sal max_sal_25 max_sal_50 max_sal_75
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
KING 5000 10 0 5000 3725 2450 1875
CLARK 2450 10.5 5000 3725 2450 1875
MILLER 1300 10 1 5000 3725 2450 1875
SCOTT 3000 20 0 3000 3000 2975 1100
FORD 3000 20 0 3000 3000 2975 1100
JONES 2975 20.5 3000 3000 2975 1100
ADAMS 1100 20.75 3000 3000 2975 1100
SMITH 800 20 1 3000 3000 2975 1100
BLAKE 2850 30 0 2850 1575 1375 1250
ALLEN 1600 30.2 2850 1575 1375 1250
TURNER 1500 30.4 2850 1575 1375 1250
WARD 1250 30.6 2850 1575 1375 1250
MARTIN 1250 30.6 2850 1575 1375 1250
JAMES 950 30 1 2850 1575 1375 1250
14 rows have been selected.


Sql> Select E.ename,e.sal,e.deptno,
Percent_rank () over (partition by Deptno ORDER by Sal) P_rank,
Percentile_cont (0) within group (order by Sal)
Over (partition by Deptno) Max_sal,
Percentile_cont (0.25) within group (order by Sal)
Over (partition by Deptno) max_sal_25,
Percentile_cont (0.5) within group (order by Sal)
Over (partition by Deptno) Max_sal_50,
Percentile_cont (0.75) within group (order by Sal)
Over (partition by Deptno) max_sal_75
From EMP E;
ename SAL DEPTNO p_rank max_sal max_sal_25 max_sal_50 max_sal_75
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
MILLER 1300 10 0 1300 1875 2450 3725
CLARK 2450 10.5 1300 1875 2450 3725
KING 5000 10 1 1300 1875 2450 3725
SMITH 800 20 0 800 1100 2975 3000
ADAMS 1100 20.25 800 1100 2975 3000
JONES 2975 20.5 800 1100 2975 3000
SCOTT 3000 20.75 800 1100 2975 3000
FORD 3000 20.75 800 1100 2975 3000
JAMES 950 30 0 950 1250 1375 1575
MARTIN 1250 30.2 950 1250 1375 1575
WARD 1250 30.2 950 1250 1375 1575
TURNER 1500 30.6 950 1250 1375 1575
ALLEN 1600 30.8 950 1250 1375 1575
BLAKE 2850 30 1 950 1250 1375 1575
14 rows have been selected.

Oracle Analytic function Percentile_cont

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.