oracle sql rank dense_rank row_number fisrt last
測試表emp
RANK
SQL> select EMPNO,DEPTNO,SAL,
2 rank()over(partition by DEPTNO order by SAL) "rank"
3 from emp;
EMPNO DEPTNO SAL rank
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 4
7499 30 1600 5
7698 30 2850 6
14 rows selected.
按照deptno分組sal升序排列,這裡注意到deptno=30的sal=1250的兩個僱員,並列在該組中排名第2。然後下一個就是第4名。
What we see here is where two people have the same salary they are assigned the same rank. When multiple rows share the same rank the next rank in the sequence is not consecutive
如果兩個人有相同的salary,就分配相同的排名。多行共享了相同的排名之後,下一個排名在順序上是不連續的。
要和後面的dense_rank(),row_number()區分。
DENSE_RANK
The DENSE_RANK
function acts like the RANK
function except that it assigns consecutive ranks.
DENSE_RANK函數和RANK函數相似,如果有多個人有相同的salary,會分配same rank,不同之處在于下一個排名在順序上是連續的。
SQL> select EMPNO,DEPTNO,SAL,
2 dense_rank()over(partition by DEPTNO order by SAL) "rank"
3 from emp;
EMPNO DEPTNO SAL rank
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 3
7499 30 1600 4
7698 30 2850 5
14 rows selected.
ROW_NUMBER
row_number函數的區別在於,當多個人有相同的salary的時候并不會共享rank,而是連續的分配rank。not same rank,not consecutive
SQL> select EMPNO,DEPTNO,SAL,
2 row_number()over(partition by DEPTNO order by SAL) "rank"
3 from emp;
EMPNO DEPTNO SAL rank
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 5
7900 30 950 1
7654 30 1250 2
7521 30 1250 3
7844 30 1500 4
7499 30 1600 5
7698 30 2850 6
14 rows selected.
FIRST and LAST
The FIRST
and LAST
functions can be used to return the first or last value from an ordered sequence
用於返回first 或者last value
SQL> select EMPNO,DEPTNO,SAL,
2 min(sal)keep(dense_rank first order by sal )over(partition by DEPTNO) "Lowest",
3 max(sal)keep(dense_rank last order by sal )over(partition by DEPTNO) "Highest"
4 from emp;
EMPNO DEPTNO SAL Lowest Highest
---------- ---------- ---------- ---------- ----------
7782 10 2450 1300 5000
7839 10 5000 1300 5000
7934 10 1300 1300 5000
7566 20 2975 800 3000
7902 20 3000 800 3000
7876 20 1100 800 3000
7369 20 800 800 3000
7788 20 3000 800 3000
7521 30 1250 950 2850
7844 30 1500 950 2850
7499 30 1600 950 2850
7900 30 950 950 2850
7698 30 2850 950 2850
7654 30 1250 950 2850
14 rows selected.
下面這種寫法,返回結果和上面一樣。
SQL> select EMPNO,DEPTNO,SAL,
2 min(sal)over(partition by DEPTNO) "Lowest",
3 max(sal)over(partition by DEPTNO) "Highest"
4 from emp;
EMPNO DEPTNO SAL Lowest Highest
---------- ---------- ---------- ---------- ----------
7782 10 2450 1300 5000
7839 10 5000 1300 5000
7934 10 1300 1300 5000
7566 20 2975 800 3000
7902 20 3000 800 3000
7876 20 1100 800 3000
7369 20 800 800 3000
7788 20 3000 800 3000
7521 30 1250 950 2850
7844 30 1500 950 2850
7499 30 1600 950 2850
7900 30 950 950 2850
7698 30 2850 950 2850
7654 30 1250 950 2850
14 rows selected.
2016-04-13
oracle sql rank dense_rank row_number fisrt last的更多相关文章
- [转]oracle分析函数Rank, Dense_rank, row_number
oracle分析函数Rank, Dense_rank, row_number 分析函数2(Rank, Dense_rank, row_number) 目录 ==================== ...
- Oracle分析函数 — rank, dense_rank, row_number用法
本文通过例子演示了Oracle分析函数 —— rank, dense_rank, row_number的用法. //首先建score表 create table score( course nva ...
- oracle分析函数Rank, Dense_rank, row_number
http://www.cnblogs.com/wuyisky/archive/2010/02/24/oracle_rank.html 目录=============================== ...
- Oracle 的开窗函数 rank,dense_rank,row_number
1.开窗函数和分组函数的区别 分组函数是指按照某列或者某些列分组后进行某种计算,比如计数,求和等聚合函数进行计算. 开窗函数是指基于某列或某些列让数据有序,数据行数和原始数据数相同,依然能曾现个体数据 ...
- rank,dense_rank,row_number使用和区别
rank,dense_rank,row_number区别 一:语法(用法): rank() over([partition by col1] order by col2) dense ...
- 【DB2】DB2中rank(),dense_rank(),row_number()的用法
1.准备测试数据 DROP TABLE oliver_1; ),SUB_NO ),SCORE int); ,,); ,,); ,,); ,,); ,,); ,,); 2.详解rank(),dense_ ...
- [z]一个SQL语句分清楚RANK(),DENSE_RANK(),ROW_NUMBER()三个排序的不同
转自:http://blog.csdn.net/s630730701/article/details/51902762 在SCOTT用户下,执行下面SQL; SELECT s.deptno,s.ena ...
- rank() | dense_rank() | row_number() over(PARTITION BY sex order by age desc ) 的区别
1.row_num() over()函数:根据某个字段排序后编号1,2,3.. select *,ROW_NUMBER() over ( order by majorid) as numfrom St ...
- Oracle 排序分析函数之ROW_NUMBER、RANK和DENSE_RANK
我们都知道分析函数功能很强大,可能需要写很复杂的标准SQL才能办到或不可能办到的事,使用分析函数却能很容易完成.我们经常会用到排序分析函数,如ROW_NUMBER,RANK,DENSE_RANK.这三 ...
随机推荐
- 使用jar命令打war包
1.打开cmd进入web项目发布文件夹 2.,输入jar -cvf qxpt.war * (*表示当前目录下所有子目录) 3,回车等待执行完成就可以了 4.如果web项目发布文件夹有多个文件夹,而打w ...
- Extjs 组件共用(单例)问题
说明: 将store初始化在类定义时便创建, store实例将成为该类的单例 代码: 测试: 说明: 将store初始化放入initComponent函数中. 每次都将创建一个新的实例. 代码: 测 ...
- iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要 ...
- curl api create domain
from: https://adam.younglogic.com/2013/09/keystone-v3-api-examples/ http://docs.openstack.org/develo ...
- ls常用选项总结
参考: http://billie66.github.io/TLCL/book/index.html ls - List directory contents 选项 长选项 描述 -a --all 列 ...
- 深入理解 Win32 PE 文件格式
深入理解 Win32 PE 文件格式 Matt Pietrek 这篇文章假定你熟悉C++和Win32. 概述 理解可移植可执行文件格式(PE)可以更好地了解操作系统.如果你知道DLL和EXE中都有些什 ...
- ios 使用autolayout 后button 的frame 无法设置问题!
问题见这里,只能通过bounds和center进行设置!http://www.cocoachina.com/bbs/read.php?tid-236862.html 待研究!!!!~~~
- UIImage 在某些控件上被放大问题
今天用到了UISlider,想利用slider.setThumbImage(UIImage(named:"aaa"), forState: UIControlState.Norma ...
- 关于MySQL count(distinct) 逻辑的一个bug【转】
本文来自:http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误,实际结果存在值,但是用cou ...
- code vs 1026 逃跑的拉尔夫
1026 逃跑的拉尔夫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走 ...