ORACLE窗口函数
--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;
ORACLE窗口函数的更多相关文章
- oracle窗口函数中range interval的使用
oracle窗口函数中range interval配合一般用来针对指定时间范围进行统计.其中range表示范围,between...and 表示之前的范围和之后的范围 , CURRENT ROW表示当 ...
- Oracle窗口函数显示想要的行数
Oracle中支持窗口函数ROW_NUMBER(),其用法和 MSSQLServer2005中相同,比如我们 执行下面的 SQL语句: SELECT * FROM ( SELECT ROW_NUMBE ...
- oracle 窗口函数over
select *,sum(字段) over()from table :--相加所有行这个字段的和 select max(字段1),over(partition by 字段2,字段3) from ta ...
- oracle 窗口函数 (keep)
看到很多人对于keep不理解,这里解释一下! Returns the row ranked first using DENSE_RANK2种取值:DENSE_RANK FIRSTDENSE_RANK ...
- Oracle树形结构数据---常见处理情景
Oracle树形结构数据---常见处理情景 1.查看表数据结构 SELECT * FROM QIANCODE.TREE_HIS_TABLE T ORDER BY T.NODE_LEVEL; ...
- Hive新功能 Cube, Rollup介绍
说明:Hive之cube.rollup,还有窗口函数,在传统关系型数据(Oracle.sqlserver)中都是有的,用法都很相似. GROUPING SETS GROUPING SETS作为GROU ...
- SQL金典
ps:补充自己的基础知识,大神请无视.. ~~~~~~~~~~~~~~~~~~~~~ DataBase Management System,DBMS.... Catalog ...库 Table... ...
- LeetCode:180.连续出现的数字
题目链接:https://leetcode-cn.com/problems/consecutive-numbers/ 题目 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+--- ...
- Oracle开发之窗口函数 rows between unbounded preceding and current row
目录=========================================1.窗口函数简介2.窗口函数示例-全统计3.窗口函数进阶-滚动统计(累积/均值)4.窗口函数进阶-根据时间范围统计 ...
随机推荐
- Spring boot 自定义拦截器
1.新建一个类实现HandlerInterceptor接口,重写接口的方法 package com.zpark.interceptor; import com.zpark.tools.Constant ...
- 导入myeclipse的java源码查看不了的问题
导入之前自己的jar包后 ,可以正常使用了,但是发现按ctrl+鼠标左键查看不了源代码.attach source 来源后,还是没有效果. 先添加所要使用的jar包, 然后再添加源文件.最后终于显示成 ...
- PHP PDF文件上传
/** * 上传PDF文件 */ function UploadPdf(){ if(is_array($_FILES)){ $tmp_file = $_FILES['pdf'] ['tmp_name' ...
- django之注册登录
清理session数据,自此django的认证登陆登出功能完成,但是此处有个问题,就是当用户在手动关闭浏览器的时候,session数据不会自动失效,数据库的session数据也不会自动删除,所以需要在 ...
- openssl升级
红帽6.2升级openssl方法 yum安装nginx时,发现openssl依赖包错误,提示openssl要求版本为1.0.1 ,但当前版本为1.0.0.通过网上介绍的办法,将openssl源码重新编 ...
- Django05-模型系统model
Object Relational Mapping(ORM) 一.ORM介绍 1.ORM概念对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关 ...
- laravel框架memcached的使用
在laravel配置及使用使用 Memcached 缓存要求安装了Memcached PECL 包,即 PHP Memcached 扩展.你可以在配置文件 config/cache.php 中列出所有 ...
- Juc中Atomic原子类总结
1 Atomic原子类介绍 2 基本类型原子类 3 数组类型原子类 4 引用类型原子类 5 对象的属性修改类型原子类
- IDEA与eclipse:vm参数调优笔记
我的电脑配置12G内存,64位,win10系统. 首先,idea,是在idea中安装目录下的bin目录下面找到位的同学注意找到idea32.exe.vmoptions的文件去编辑. 更改文件,无非大多 ...
- elasticsearch增删查改
创建结构化索引 put http://127.0.0.1:9200/person{ "settings" : { "number_of_shards": 3, ...