ROW_NUMBER() OVER函数的基本用法

我们两清 提交于 2020-04-08 10:34:41
select * from (  select * ,row_number() over(partition by DDID order by DDID desc) y  from LDZBSCJH where HTBH='V01461' )  as b  where y <>1

SqlServer四种排序:ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()

先看一下最简单的sql语句以及最原始的效果图:

SELECT a.userName,a.userid FROM dbo.t_user a

【四种排序方式】

1.ROW_NUMBER()排序

我们将这几行数据进行简单的排序,另给一个新的列RN

SELECT a.userName,a.userid,ROW_NUMBER()OVER	(order by a.userName) AS RN FROM dbo.t_user a

这时,我想针对名字这一列,将重复的数据以1开头排序,不存在重复的就是1

SELECT a.userName,a.userid,ROW_NUMBER()OVER	(partition by a.userName ORDER by a.userName) AS RN FROM dbo.t_user a

2.RANK()排序

SELECT a.userName,a.userid,RANK()OVER	(ORDER by a.userName) AS RN FROM dbo.t_user a

这时排序的应该为3的值,因为出现重复数据,所以也变成了2,但是最后的行数和最后编号的值是一样的。

3.DENSE_RANK()排序

SELECT a.userName,a.userid,DENSE_RANK()over	(ORDER by a.userName) AS RN FROM dbo.t_user a

保持了重复的数据RN值相同,但值得注意的是第4行数据变成了3,也就是说行数和最后的编号是发生了变化的。

SELECT a.userName,a.userid,NTILE(2)over	(ORDER by a.userName) AS RN FROM dbo.t_user a

 

3.NTILE()排序

 

括号里面是几,就会分成几组

例如5行数据,分成2组,就会前面3个,后面2个,默认把多余的行分给前面;分成3组,第一组就会2个,第二组就会2个,第三组就会1个

SELECT a.userName,a.userid,NTILE(2)over	(ORDER by a.userName) AS RN FROM dbo.t_user a

SELECT a.userName,a.userid,NTILE(3)over	(ORDER by a.userName) AS RN FROM dbo.t_user a
 

 

 

 

 

 

 

 

 

 

 

 

 

 

ROW_NUMBER() OVER函数的基本用法

ROW_NUMBER() OVER函数的基本用法

语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)

 

方法/步骤

 

简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。 

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

实例:

初始化数据

create table employee (empid int ,deptid int ,salary decimal(10,2))insert into employee values(1,10,5500.00)insert into employee values(2,10,4500.00)insert into employee values(3,20,1900.00)insert into employee values(4,20,4800.00)insert into employee values(5,40,6500.00)insert into employee values(6,40,14500.00)insert into employee values(7,40,44500.00)insert into employee values(8,50,6500.00)insert into employee values(9,50,7500.00)

数据显示为

需求:根据部门分组,显示每个部门的工资等级

预期结果:

SQL脚本:

SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee

 

 

 

 

ROW_NUMBER() OVER函数的基本用法

语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)

 

方法/步骤

 

简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。 

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

实例:

初始化数据

create table employee (empid int ,deptid int ,salary decimal(10,2))insert into employee values(1,10,5500.00)insert into employee values(2,10,4500.00)insert into employee values(3,20,1900.00)insert into employee values(4,20,4800.00)insert into employee values(5,40,6500.00)insert into employee values(6,40,14500.00)insert into employee values(7,40,44500.00)insert into employee values(8,50,6500.00)insert into employee values(9,50,7500.00)

数据显示为

需求:根据部门分组,显示每个部门的工资等级

预期结果:

SQL脚本:

SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!