row_number() over (partition by....order by...)用法 分组排序

转载来源:http://www.cnblogs.com/Kazaf/archive/2011/06/30/2094015.html

row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

SELECT G.*, ROW_NUMBER() OVER(PARTITION BY a ,b ORDER BY c DESC) ROWN
from (
    select ‘1‘ a, ‘2‘ b, ‘1‘ c from dual
    union all
    select ‘1‘, ‘2‘, ‘2‘ from dual
    union all
    select ‘1‘, ‘3‘, ‘3‘ from dual
    union all
    select ‘1‘, ‘3‘, ‘4‘ from dual
    union all
    select ‘1‘, ‘4‘, ‘5‘ from dual
    union all
    select ‘1‘,‘3‘,‘5‘from dual) G

  执行结果:

    A B C       ROWN

    - - - ----------
    1 2 2          1
    1 2 1          2
    1 3 5          1
    1 3 4          2
    1 3 3          3
    1 4 5          1

  与rownum的区别在于:使用rownum进行排序的时候是先对结果集加入伪列rownum然后再进行排序,而此函数在包含排序从句后是先排序再计算行号码.

    row_number()和rownum差不多,功能更强一点(可以在各个分组内从1开时排序).

  rank()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内).

  dense_rank()l是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的 .

  lag(arg1,arg2,arg3): 
    arg1是从其他行返回的表达式 
    arg2是希望检索的当前行分区的偏移量。是一个正的偏移量,时一个往回检索以前的行的数目。 
    arg3是在arg2表示的数目超出了分组的范围时返回的值。

语句一:

select row_number() over(order by sale/cnt desc) as sort, sale/cnt
from (
    select -60 as sale,3 as cnt from dual union
    select 24 as sale,6 as cnt from dual union
    select 50 as sale,5 as cnt from dual union
    select -20 as sale,2 as cnt from dual union
    select 40 as sale,8 as cnt from dual);

  执行结果:

SORT       SALE/CNT
    ---------- ----------
             1             10
             2              5
             3              4
             4            -10
             5            -20

语句二: 查询员工的工资,按部门排序

select ename,sal,row_number() over (partition by deptno order by sal desc) as sal_order
from scott.emp;

  执行结果:

    ENAME                           SAL      SAL_ORDER
    -------------------- ---------- ----------
    KING                           5000              1
    CLARK                          2450              2
    MILLER                         1300              3
    SCOTT                          3000              1
    FORD                           3000              2
    JONES                          2975              3
    ADAMS                          1100              4
    SMITH                           800              5
    BLAKE                          2850              1
    ALLEN                          1600              2
    TURNER                         1500              3
    WARD                           1250              4
    MARTIN                         1250              5
    JAMES                           950              6

    已选择14行。

语句三: 查询每个部门的最高工资

select deptno,ename,sal from
     (select deptno,ename,sal,row_number() over (partition by deptno order by sal desc) as sal_order
         from scott.emp) where sal_order <2;

  执行结果:

  DEPTNO ENAME                 SAL
    ---------- -------------------- ----------
             10 KING                      5000
             20 SCOTT                    3000
             30 BLAKE                    2850

    已选择3行。

语句四:rank()

select deptno,sal,rank() over (partition by deptno order by sal) as rank_order from scott.emp order by deptno;

  执行结果:

  DEPTNO         SAL RANK_ORDER
    ---------- ---------- ----------
           10        1300           1
           10        2450           2
           10        5000           3
           20         800           1
           20        1100           2
           20        2975           3
           20        3000           4
           20        3000           4
           30         950           1
           30        1250           2
           30        1250           2
           30        1500           4
           30        1600           5
           30        2850           6

    已选择14行。

语句五:dense_rank()

select deptno,sal,dense_rank() over(partition by deptno order by sal) as dense_rank_order from scott.emp order by deptn;

  执行结果:

  DEPTNO         SAL DENSE_RANK_ORDER
    ---------- ---------- ----------------
           10        1300                 1
           10        2450                 2
           10        5000                 3
           20         800                 1
           20        1100                 2
           20        2975                 3
           20        3000                 4
           20        3000                 4
           30         950                  1
           30        1250                 2
           30        1250                 2
           30        1500                 3
           30        1600                 4
           30        2850                 5

  已选择14行。

语句六:lag(ename,1,null)

select deptno,ename,sal,lag(ename,1,null) over(partition by deptno order by ename) as lag_ from scott.emp order by deptno;

  执行结果:

  DEPTNO ENAME                        SAL LAG_
    ---------- -------------------- ---------- --------------------
           10 CLARK                       2450
           10 KING                        5000 CLARK
           10 MILLER                      1300 KING
           20 ADAMS                       1100
           20 FORD                        3000 ADAMS
           20 JONES                       2975 FORD
           20 SCOTT                       3000 JONES
           20 SMITH                        800 SCOTT
           30 ALLEN                       1600
           30 BLAKE                       2850 ALLEN
           30 JAMES                        950 BLAKE
           30 MARTIN                      1250 JAMES
           30 TURNER                      1500 MARTIN
           30 WARD                        1250 TURNER

    已选择14行。

时间: 2024-11-03 05:43:58

row_number() over (partition by....order by...)用法 分组排序的相关文章

Oracle中的 row_number() over (partition by order by ) 用法

oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_name;; 这句话的意思是把表中的数值按照colx 分组,每一组内部按照coly排序,同时 row_number()返回排序之后该记录在改组内部的序号. 比如我们知道有emp表如下: SQL> SELECT * FROM SCOTT.EMP; EMPNO ENAME JOB MGR HIREDATE

ROW_NUMBER() OVER(PARTITION BY ORDER BY )RN 只选一行

select PEGGED_SO_ID,PEGGED_SO_LINE_ID ,ITEM_ID ,QUANTITY ,LOCATION ,SITEID from IN_SEMI_ONHAND_LOCKED where item_id in ('000000101011003137','000000102011000038') ; SELECT DISTINCT PEGGED_SO_ID,PEGGED_SO_LINE_ID ,ITEM_ID ,QUANTITY ,LOCATION ,SITEID ,

oracle查询中over(partition by ...order by ...)用法

例: rank() over(partition by deptno order by sal desc) rank 解释: deptno -- 部门, sal -- 工资 over:  在什么条件之上.partition by e.deptno:  按部门编号划分(分区).order by e.sal desc:  按工资从高到低排序(使用rank()/dense_rank() 时,必须要带order by否则非法)rank():  分级 整个语句的意思就是:在按部门划分的基础上,按工资从高到

【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例

原文:[SQL]ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例 #用法说明 select row_number() over(partition by A order by B ) as rowIndex from table A :为分组字段 B:为分组后的排序字段. table 表的结构 多为:  多人 多条的相关数据.(比如:订单信息) 此条sql语句,多用于对数据进行分组排序,并对每个组中的数据分别进行编号,编号从1开始

ROW_NUMBER() OVER()函数用法;(分组,排序),partition by

1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where group by  order by 的执行. partition by 用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组,它和聚合函数不同的地方在于它能够返回一个分组中的多条记录,而聚合函数一般只有一个反映统计值的记录. 例如:employee,根据部门分组排

去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

关键字  ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 按照分组字段进行排序并标编号 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 语法 ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN 实例 SELECT XMJL_UID,XMJL,XMCREATETIME,ROW_NUMBER() OVE

分组 根据某一列进行排序,根据shopid分组,用createTime排序,返回row_number()序号 select no =row_number() over (partition by shopId order by createTime desc), * from Goods_info

over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by columnname2)含义:按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序.例如:employees表中,有两个部门的记录:department_id =10和20select department_id,rank() over(partition

row_number() over(partition by a order by b desc) rn 用法

ow_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) 实际应用 取最新的数据 select * from (select hlpr.emplid, hlpr.deptid, hlpr.create_time, row_number() over (partition by hlpr.emplid order by hlpr.cre

Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序

--//创建一个信息表 CREATE TABLE user_student(id decimal(18,0) identity(1,1),st_name nvarchar(30),class nvarchar(10),score decimal(18,2)) --//插入测试数据============start=================== insert into user_student(st_name,class,score) values('张三','甲','90') inser