ORACLE窗口函数

时间:2021-10-25 06:29:48

--ORACLE窗口函数,是针对分析用的。

--create table
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)
);
alter table EMP
add constraint PK_EMP primary key (EMPNO);

--insert data
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, 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, 300, 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, 500, 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, 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, 1400, 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, 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, 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, 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, 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, 0, 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, 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, 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, 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, null, 10);

SELECT * FROM EMP

--查询每个部门的最高工资的员工信息
select empno, ename, job, hiredate, sal, deptno
from (select empno, ename, job, hiredate, sal, deptno, rank() over(partition by deptno order by sal desc) r from emp)
where r = 1

--查询每个部门的最低工资的员工信息
select empno, ename, job, hiredate, sal, deptno
from (select empno, ename, job, hiredate, sal, deptno, rank() over(partition by deptno order by sal) r from emp)
where r = 1

--查询每个部门员工的信息,以及本部门的最高工资,最低工资,高一位的工资,低一位的工资
SELECT EMPNO,ENAME,JOB,HIREDATE,SAL,DEPTNO,
MAX(SAL) OVER(PARTITION BY DEPTNO) MAX_SAL, --最高工资
MIN(SAL) OVER(PARTITION BY DEPTNO) MIN_SAL, --最低工资
LEAD(SAL,1,0) OVER(PARTITION BY DEPTNO ORDER BY SAL) LEAD_SAL, --高1位的工资
LAG(SAL,1,0) OVER(PARTITION BY DEPTNO ORDER BY SAL) LAG_SAL, --低1位的工资
LEAD(SAL,2,0) OVER(PARTITION BY DEPTNO ORDER BY SAL) LEAD_SAL2, --高2位的工资
LAG(SAL,2,0) OVER(PARTITION BY DEPTNO ORDER BY SAL) LAG_SAL2 --低2位的工资
FROM EMP

--
select ename 姓名, job 职业, sal 工资, deptno 部门,sal,
first_value(sal) over(partition by deptno) first_sal,
last_value(sal) over(partition by deptno) last_sal,
sum(sal) over(partition by deptno) 部门总工资,
round(avg(sal) over(partition by deptno),1) 部门平均工资,
count(1) over(partition by deptno) 部门总数,
row_number() over(partition by deptno order by sal) 序号
from emp;

select ename 姓名, job 职业, sal 工资, deptno 部门,sal,
first_value(sal) over(partition by deptno) first_sal,
last_value(sal) over(partition by deptno) last_sal,
row_number() over(partition by deptno order by sal) 序号
from emp;

--查询员工工资,按部门排序
select ename,deptno,sal,row_number() over (partition by deptno order by sal desc) as sal_order from emp
select ename,deptno,sal,rank() over (partition by deptno order by sal desc) as sal_order from emp

select deptno,ename,sal from
(select deptno,ename,sal,rank() over (partition by deptno order by sal desc) as sal_order
from emp) where sal_order=1;