深入探讨:oracle中row_number() over()分析函数用法
程序员文章站
2023-12-01 23:23:58
row_number()over(partition by col1 order by col2)表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每...
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);
语法格式:
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
与rownum的区别在于:使用rownum进行排序的时候是先对结果集加入伪劣rownum然后再进行排序,而此函数在包含排序从句后是先排序再计算行号码。
row_number()和rownum差不多,功能更强一点(可以在各个分组内从1开始排序)。
rank()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内)
dense_rank()也是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的。
oracle 分析函数 row_number(),返回一个整数值(>=1);
语法格式:
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
下一篇: C#实现redis读写的方法