測試表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的更多相关文章

  1. [转]oracle分析函数Rank, Dense_rank, row_number

    oracle分析函数Rank, Dense_rank, row_number 分析函数2(Rank, Dense_rank, row_number)   目录 ==================== ...

  2. Oracle分析函数 — rank, dense_rank, row_number用法

    本文通过例子演示了Oracle分析函数 —— rank, dense_rank, row_number的用法. //首先建score表 create table score( course   nva ...

  3. oracle分析函数Rank, Dense_rank, row_number

    http://www.cnblogs.com/wuyisky/archive/2010/02/24/oracle_rank.html 目录=============================== ...

  4. Oracle 的开窗函数 rank,dense_rank,row_number

    1.开窗函数和分组函数的区别 分组函数是指按照某列或者某些列分组后进行某种计算,比如计数,求和等聚合函数进行计算. 开窗函数是指基于某列或某些列让数据有序,数据行数和原始数据数相同,依然能曾现个体数据 ...

  5. rank,dense_rank,row_number使用和区别

    rank,dense_rank,row_number区别 一:语法(用法):     rank() over([partition by col1] order by col2)      dense ...

  6. 【DB2】DB2中rank(),dense_rank(),row_number()的用法

    1.准备测试数据 DROP TABLE oliver_1; ),SUB_NO ),SCORE int); ,,); ,,); ,,); ,,); ,,); ,,); 2.详解rank(),dense_ ...

  7. [z]一个SQL语句分清楚RANK(),DENSE_RANK(),ROW_NUMBER()三个排序的不同

    转自:http://blog.csdn.net/s630730701/article/details/51902762 在SCOTT用户下,执行下面SQL; SELECT s.deptno,s.ena ...

  8. 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 ...

  9. Oracle 排序分析函数之ROW_NUMBER、RANK和DENSE_RANK

    我们都知道分析函数功能很强大,可能需要写很复杂的标准SQL才能办到或不可能办到的事,使用分析函数却能很容易完成.我们经常会用到排序分析函数,如ROW_NUMBER,RANK,DENSE_RANK.这三 ...

随机推荐

  1. 7.4---加法替代运算(CC150)

    注意:1,除法那里b+=b是错的.b一直在改变.   2,仔细一点. import java.util.*; public class AddSubstitution { public int cal ...

  2. non

    I p(I q){r p(c((q>9?q-p(q/10):q)+'0')),q*10; }

  3. .pyc文件是什么?

    一个.py文件就是一个模块,而模块名就是文件名,如module.py的模块名就是module.如果module.py文件里定义了一些函数和变量,而外部文件如test_module.py想使用这些函数或 ...

  4. CentOS用yum安装、配置MariaDB

    .创建/etc/yum.repos.d/MariaDB.repo文件,这里用到了刚刚发布正式版的10. [mariadb] name = MariaDB baseurl = http://yum.ma ...

  5. 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战

    转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...

  6. hadoop初识

    搞什么东西之前,第一步是要知道What(是什么),然后是Why(为什么),最后才是How(怎么做).但很多开发的朋友在做了多年项目以后,都习惯是先How,然后What,最后才是Why,这样只会让自己变 ...

  7. ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)

    两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...

  8. 算法手记 之 数据结构(并查集详解)(POJ1703)

    <ACM/ICPC算法训练教程>读书笔记-这一次补上并查集的部分.将对并查集的思想进行详细阐述,并附上本人AC掉POJ1703的Code. 在一些有N个元素的集合应用问题中,通常会将每个元 ...

  9. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. Spring4 与 Hibernate4 整合过程中的问题记录

    Spring4使用注解配置,很方便也很有趣,就是有一些坑需要自己去发现和解决,笔者列出自己在使用过程中遇到的问题,希望对您有所帮助. 1.如果使用hibernate.cfg.xml配置文件配置Hibe ...