SQL Server - 四种排序, ROW_NUMBER() /RANK() /DENSE_RANK() /ntile() over()
>>>>英文版 (更简洁易懂)<<<<
转载自:https://dzone.com/articles/difference-between-rownumber
One of the most obvious and useful set of window functions are ranking functions where rows from your result set are ranked according to a certain scheme. There are three ranking functions:
ROW_NUMBER()RANK()DENSE_RANK()
The difference is easy to remember. For the examples, let’s assume we have this table (using PostgreSQL syntax):
CREATE TABLE t(v) AS
SELECT * FROM (
VALUES('a'),('a'),('a'),('b'),
('c'),('c'),('d'),('e')
) t(v)
ROW_NUMBER()
… assigns unique numbers to each row within the PARTITION given the ORDER BY clause. So you’d get:
SELECT v, ROW_NUMBER() OVER()
FROM t
Note that some SQL dialects (e.g. SQL Server) require an explicit ORDER BY clause in the OVER() clause:
SELECT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
The above query returns:

RANK()
… behaves like ROW_NUMBER(), except that “equal” rows are ranked the same. If we substitute RANK() into our previous query:
SELECT v, RANK() OVER(ORDER BY v)
FROM t
… then the result we’re getting is this:

As you can see, much like in a sports ranking, we have gaps between the different ranks. We can avoid those gaps by using
DENSE_RANK()
Trivially, DENSE_RANK() is a rank with no gaps, i.e. it is “dense”. We can write:
SELECT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain

One interesting aspect of DENSE_RANK() is the fact that it “behaves like” ROW_NUMBER()when we add the DISTINCT keyword.
SELECT DISTINCT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain

In fact, ROW_NUMBER() prevents you from using DISTINCT, because ROW_NUMBER()generates unique values across the partition beforeDISTINCT is applied:
SELECT DISTINCT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
DISTINCT has no effect:



Putting it all together
A good way to understand the three ranking functions is to see them all in action side-by-side. Run this query
SELECT
v,
ROW_NUMBER() OVER(ORDER BY v),
RANK() OVER(ORDER BY v),
DENSE_RANK() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
… to obtain:

Note that unfortunately, the WINDOW clause is not supported in all databases.
>>>>中文版<<<<
转载自:https://www.cnblogs.com/SunnyZhu/p/5762898.html
SqlServer的四种排序,当场写了几句Sql让她了解,现把相关Sql放上来。
首先,我们创建一些测试数据。

if OBJECT_ID('Tempdb.dbo.#Tmp') is not null
drop table #Tmp
create table #Tmp
(
name nvarchar(10)
)
insert into #Tmp
select N'张三'
union
select N'李四'
union
select N'王五'
union
select N'赵六'
union
select N'朱七'
union
select N'王八'
union all
select N'张三'

最后一个union用union all,因为我们多一行"张三"。
一、ROW_NUMBER() over(partition by columnname order by columnname)
select ROW_NUMBER()over(order by name) as num,* from #Tmp
可以得到按name排序的结果集。

ROW_NUMBER() over()还有一种用法,可以针对某列进行分组排序。
下面结果可以看到张三有1和2两个排序,而其他的名字排序都只有1。
select ROW_NUMBER()over(partition by name order by name) as num,* from #Tmp

二、RANK()over(order by columnname)
大家可以从下面的结果集看到,结果集少了5的编号,而有两个4的编号,然后直接跳到编号6。
select RANK()over(order by name),* from #Tmp

三、DENSE_RANK()over(order by columnname)
select DENSE_RANK()over(order by name),* from #Tmp
执行Sql后发现,下面的结果集有2个编号4的行,紧接着就是编号5的行。
DENSE_RANK()函数和RANK()函数差不多。
RANK()函数不管分几组,最后的编号一定和行数相同。
DENSE_RANK()函数最后的编号和分组的数目有关。

四、NTILE()OVER(ORDER BY COLUMNNAME)
select NTILE(2)over(order by name),* from #Tmp
select NTILE(3)over(order by name),* from #Tmp
NTILE后面的数字,是要把查询得到的结果平均分为几组。
如下图分为2和3组。
如果行数平均划分后还有余行,那么就把行分在最前面的几组上。
比如我们的结果有7行,要分为3组。
那么第一组3行,第二组2行,第三组2行。
如果我们结果有14行,平均分为3组。
那么第一组5行,第二组5行,第三组4行。
依此类推。

One of the most obvious and useful set of window functions are ranking functions where rows from your result set are ranked according to a certain scheme. There are three ranking functions:
ROW_NUMBER()RANK()DENSE_RANK()
The difference is easy to remember. For the examples, let’s assume we have this table (using PostgreSQL syntax):
CREATE TABLE t(v) AS
SELECT * FROM (
VALUES('a'),('a'),('a'),('b'),
('c'),('c'),('d'),('e')
) t(v)
ROW_NUMBER()
… assigns unique numbers to each row within the PARTITION given the ORDER BY clause. So you’d get:
SELECT v, ROW_NUMBER() OVER()
FROM t
Note that some SQL dialects (e.g. SQL Server) require an explicit ORDER BY clause in the OVER() clause:
SELECT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
The above query returns:

RANK()
… behaves like ROW_NUMBER(), except that “equal” rows are ranked the same. If we substitute RANK() into our previous query:
SELECT v, RANK() OVER(ORDER BY v)
FROM t
… then the result we’re getting is this:

As you can see, much like in a sports ranking, we have gaps between the different ranks. We can avoid those gaps by using
DENSE_RANK()
Trivially, DENSE_RANK() is a rank with no gaps, i.e. it is “dense”. We can write:
SELECT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain

One interesting aspect of DENSE_RANK() is the fact that it “behaves like” ROW_NUMBER()when we add the DISTINCT keyword.
SELECT DISTINCT v, DENSE_RANK() OVER(ORDER BY v)
FROM t
… to obtain

In fact, ROW_NUMBER() prevents you from using DISTINCT, because ROW_NUMBER()generates unique values across the partition beforeDISTINCT is applied:
SELECT DISTINCT v, ROW_NUMBER() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
DISTINCT has no effect:



Putting it all together
A good way to understand the three ranking functions is to see them all in action side-by-side. Run this query
SELECT
v,
ROW_NUMBER() OVER(ORDER BY v),
RANK() OVER(ORDER BY v),
DENSE_RANK() OVER(ORDER BY v)
FROM t
ORDER BY 1, 2
… or this one (using the SQL standard WINDOW clause, to reuse window specifications):
SELECT
v,
ROW_NUMBER() OVER(w),
RANK() OVER(w),
DENSE_RANK() OVER(w)
FROM t
WINDOW w AS (ORDER BY v)
… to obtain:

Note that unfortunately, the WINDOW clause is not supported in all databases.
SQL Server - 四种排序, ROW_NUMBER() /RANK() /DENSE_RANK() /ntile() over()的更多相关文章
- SQL Server中排名函数row_number,rank,dense_rank,ntile详解
SQL Server中排名函数row_number,rank,dense_rank,ntile详解 从SQL SERVER2005开始,SQL SERVER新增了四个排名函数,分别如下:1.row_n ...
- SQL Server:排名函数row_number,rank,dense_rank,ntile详解
1.Row_Number函数 row_number函数大家比较熟悉一些,因为它的用途非常的广泛,我们经常在分页与排序中用到它,它的功能就是在每一行中生成一个连续的不重复的序号 例如: select S ...
- ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()
ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over() 今天女票问我SqlServer的四种排序,当场写了几句Sql让她了解,现把相关Sql放上来. 首先, ...
- 【SQL】四种排序开窗函数
一 .简单了解什么是开窗函数 什么是开窗函数,开窗函数有什么作用,特征是什么? 所谓开窗函数就是定义一个行为列,简单讲,就是在你查询的结果上,直接多出一列值(可以是聚合值或是排序号),特征就是带有ov ...
- 知方可补不足~row_number,rank,dense_rank,ntile排名函数的用法
回到目录 这篇文章介绍SQL中4个很有意思的函数,我称它的行标函数,它们是row_number,rank,dense_rank和ntile,下面分别进行介绍. 一 row_number:它为数据表加一 ...
- SQL-OVER与四种排名函数:ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()
1 SELECT orderid,custid,val, ROW_NUMBER() OVER(ORDER BY val) AS rownum, RANK() OVER(ORDER BY val) AS ...
- SqlServer四种排序:ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()
首先,我们创建一些测试数据. if OBJECT_ID('Tempdb.dbo.#Tmp') is not null drop table #Tmp create table #Tmp ( name ...
- PCB MS SQL 排序应用(row_number rank dense_rank NTILE PARTITION)
一.排序前,准备数据 --表变量 ),流程数 int) insert into @table union all union all union all union all --查看一下 select ...
- sqlserver 中row_number,rank,dense_rank,ntile排名函数的用法
1.row_number() 就是行号 2.rank:类似于row_number,不同之处在于,它会对order by 的字段进行处理,如果这个字段值相同,那么,行号保持不变 3.dense_rank ...
随机推荐
- PHP性能优化:in_array和isset 在大数组查询中耗时相差巨大,以及巧妙使用array_flip
今天在PHP业务开发中,发现了一个问题. 两个较大数组(20万+元素),遍历其中一个$a,另一个数组$b用于查找元素. 比如 foreach($a as $val){ if(in_array($xx, ...
- System.getProperty System.getenv 区别 log4j取法
log4j 可以${}取系统变量相关属性 getProperty Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...
- [第二届构建之法论坛] 预培训文档(C++版)
本博客是第二届构建之法论坛暨软件工程培训活动预培训文档中[适用于结对编程部分的C++版本],需要实验者有一部分C++基础. 目录 Part0.背景 Part1.配置环境 Part2.克隆项目 Part ...
- PS调出米黄色复古柔和外景人物照
配色思路 从片中可以看出主要景物近处的有人物和栏杆,远处的海水,天空和礁石.为体现出远近层次,近处景物选择了偏黄的色调,远处景物选择了偏青色调. 调色 以下面这张照片为例,先放上对比图: LR部分 首 ...
- java学习之—实现一个简单的ArrayList
package thread1; /** * 实现一个简单的ArrayList * * @Title: uminton */ public class SimpleArrayList<T> ...
- C. Multiplicity 简单数论+dp(dp[i][j]=dp[i-1][j-1]+dp[i-1][j] 前面序列要满足才能构成后面序列)+sort
题意:给出n 个数 的序列 问 从n个数删去任意个数 删去的数后的序列b1 b2 b3 ......bk k|bk 思路: 这种题目都有一个特性 就是取到bk 的时候 需要前面有个bk-1的序列前 ...
- GDOI2018游记&题解
day0 第一件事当然是去酒店入住+领一堆东西. 感觉酒店不错,而且离学校挺近的,走路10分钟不到,骑车5分钟就到了. 然后去学校吃饭.我们在教工饭堂吃饭,饭菜还不错,但是没有筷子差评. 吃完饭后找了 ...
- vm Linux centos 链接外网
修改network配置 vi /etc/sysconfig/network-scripts/ifcfg-ens33 修改ONBOOT=yes 重启服务 service network restart ...
- 「LibreOJ NOI Round #1」验题
麻烦的动态DP写了2天 简化题意:给树,求比给定独立集字典序大k的独立集是哪一个 主要思路: k排名都是类似二分的按位确定过程. 字典序比较本质是LCP下一位,故枚举LCP,看多出来了多少个独立集,然 ...
- TypeScript体系调研报告
作者简介:aoto 蚂蚁金服·数据体验技术团队 Q:为什么要写这边文章?这篇文章要表达什么? A:我们考虑在SPA应用中使用TS作为开发语言,我们需要一篇系统性介绍TS本身及周边的文章来论证在项目中使 ...