rownum与row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

1)rownum 为查询结果排序。使用rownum进行排序的时候是先对结果集加入伪列rownum然后再进行排序

select rownum n, a.* from ps_user a order by name

2)row_number() OVER (order by name)  包含排序从句OVER(),先排序再计算行号码 

select row_number() over(order by name) n, a.* from ps_user a

两种方式结果差异如下:

 

                1)                                                              2)

可见 第一种序号为按照我们想要的方式排序,序号是乱的。也可以通过rownum实现需要的排序结果,按如下方式编写语句

select rownum n,a.* from
(
select a.* from ps_user a order by name
) a

转载:http://tagalin.iteye.com/blog/1041574

 

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

结果如下:

  

 

posted on 2014-04-27 11:29  儿时精神  阅读(1979)  评论(0编辑  收藏  举报

导航