深入探讨:oracle中row_number() over()分析函数用法

前端之家收集整理的这篇文章主要介绍了深入探讨:oracle中row_number() over()分析函数用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

row_number()over(partition by col1 order by col2)表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)。
与rownum的区别在于:使用rownum进行排序的时候是先对结果集加入伪劣rownum然后再进行排序,而此函数在包含排序从句后是先排序再计算行号码。
row_number()和rownum差不多,功能更强一点(可以在各个分组内从1开始排序)。
rank()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内)
dense_rank()也是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的。
oracle 分析函数 row_number(),返回一个整数值(>=1);

语法格式:

<FONT style="COLOR: #ff0000">1.row_number() over (order by col_1[,col_2 ...])
作用:按照col_1[,col_2 ...]排序,返回排序后的结果集,
用法有点像rownum,为每一行返回一个不相同的值:
<div class="codetitle"><a style="CURSOR: pointer" data="12134" class="copybut" id="copybut12134" onclick="doCopy('code12134')"> 代码如下:
<div class="codebody" id="code12134">
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 指定的列进行排序;
<div class="codetitle"><a style="CURSOR: pointer" data="3848" class="copybut" id="copybut3848" onclick="doCopy('code3848')"> 代码如下:
<div class="codebody" id="code3848">
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进行排序,才得到下面的结果:
<div class="codetitle"><a style="CURSOR: pointer" data="81822" class="copybut" id="copybut81822" onclick="doCopy('code81822')"> 代码如下:
<div class="codebody" id="code81822">
A B ROW_NUMBER()OVER(ORDERBYB)
-- ---------- --------------------------
1 one 3
4 four 2
5 five 1
13 thirteen 4
22 twenty two 5

<FONT style="COLOR: #ff0000">2.row_number() over (partition by col_n[,col_m ...] order by col_1[,col_2 ...])
作用:先按照col_n[,col_m ...进行分组,
再在每个分组中按照col_1[,col_2 ...]进行排序(升序),
最后返回排好序后的结果集:
<div class="codetitle"><a style="CURSOR: pointer" data="21343" class="copybut" id="copybut21343" onclick="doCopy('code21343')"> 代码如下:
<div class="codebody" id="code21343">
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,
row_number() over (partition by c order by b) row_number
from row_number_test
order by a;

这个例子中,我们先按照c列分组,分为2组('*'组,'+'组),
再按照每个小组的b列进行排序(按字符串首字母的ascii码排),
最后按照a列排序,得到下面的结果集:
<div class="codetitle"><a style="CURSOR: pointer" data="2780" class="copybut" id="copybut2780" onclick="doCopy('code2780')"> 代码如下:<div class="codebody" id="code2780">
A B ROW_NUMBER
-- ---------- ----------
1 one 3
4 four 2
5 five 1
13 thirteen 1
22 twenty two

原文链接:https://www.f2er.com/oracle/65549.html
over()row_number()分析函数

猜你在找的Oracle相关文章