Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1124295
  • 博文数量: 159
  • 博客积分: 3063
  • 博客等级: 中校
  • 技术积分: 2703
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 01:51
文章分类

全部博文(159)

文章存档

2013年(48)

2012年(111)

分类: Oracle

2012-06-26 15:20:13

oracle中row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

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

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

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

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

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

  lag(arg1,arg2,arg3):
arg1是从其他行返回的表达式
arg2是希望检索的当前行分区的偏移量。1是一个正的偏移量,是一个往回检索以前的行的数目。
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行。

语句四:

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行。

语句五:

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行。

语句六:

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行。

-------------------------------------------------------------------------------------

再看几个例子:

语法格式:

1.row_number() over (order by col_1[,col_2 ...])

作用:按照col_1[,col_2 ...]排序,返回排序后的结果集,

此用法有点像rownum,为每一行返回一个不相同的值:

select rownum,ename,job,   
       row_number() over (order by rownum) row_number   
from emp;   
    ROWNUM ENAME      JOB       ROW_NUMBER   
---------- ---------- --------- ----------   
         1 SMITH      CLERK              1   
         2 ALLEN      SALESMAN           2   
         3 WARD       SALESMAN           3   
         4 JONES      MANAGER            4   
         5 MARTIN     SALESMAN           5   
         6 BLAKE      MANAGER            6   
         7 CLARK      MANAGER            7   
         8 SCOTT      ANALYST            8   
         9 KING       PRESIDENT          9   
        10 TURNER     SALESMAN          10   
        11 ADAMS      CLERK             11   
        12 JAMES      CLERK             12   
        13 FORD       ANALYST           13   
        14 MILLER     CLERK             14 

如果没有partition by子句, 结果集将是按照order by 指定的列进行排序;

with row_number_test as(   
     select 22 a,'twenty two' b from dual union all   
     select 1,'one' from dual union all   
     select 13,'thirteen' from dual union all   
     select 5,'five' from dual union all   
     select 4,'four' from dual)   
select a,b,   
       row_number() over (order by b)   
from row_number_test   
order by a; 

正如我们所期待的,row_number()返回按照b列排序的结果,

然后再按照a进行排序,才得到下面的结果:

A B          ROW_NUMBER()OVER(ORDERBYB)   
-- ---------- --------------------------   
1 one                                 3   
4 four                                2   
5 five                                1   
13 thirteen                           4   
22 twenty two                         5 

2.row_number() over (partition by col_n[,col_m ...] order by col_1[,col_2 ...])

作用:先按照col_n[,col_m ...进行分组,

再在每个分组中按照col_1[,col_2 ...]进行排序(升序),

最后返回排好序后的结果集:

with row_number_test as(   
     select 22 a,'twenty two' b,'*' c from dual union all   
     select 1,'one','+' from dual union all   
     select 13,'thirteen','*' from dual union all   
     select 5,'five','+' from dual union all   
     select 4,'four','+' from dual)   
select a,b,   
       row_number() over (partition by c order by b) row_number   
from row_number_test   
order by a; 
 

这个例子中,我们先按照c列分组,分为2组('*'组,'+'组),

再按照每个小组的b列进行排序(按字符串首字母的ascii码排),

最后按照a列排序,得到下面的结果集:

A B          ROW_NUMBER   
-- ---------- ----------   
1 one                 3   
4 four                2   
5 five                1   
13 thirteen           1   
22 twenty two         2


-------------------------------------------------------------------------------

统计每个字段(以各字段分组)中有多少记录的优化语句:

select count(1)
from
(
select row_number() over(partition by c1,c2,c3,c4,c5 order by c1) rw
from t1
)
where rw=1

阅读(8046) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~