首页 > 数据库 >SQL Server 中 Rank、row_number、dense_rank 三种排序函数的区别

SQL Server 中 Rank、row_number、dense_rank 三种排序函数的区别

时间:2022-11-24 19:56:45浏览次数:61  
标签:salary dense name number Rank rank employee 排序 row

现有一张工资表,需要对其进行排名,工资相同的人并列排名,然后再排名,很多刚接触的小伙伴估计第一时间想到Rank()函数或row_number() 函数,但是结果出来后并不是自己想要的,在这里就给大家介绍下排序函数dense_rank()以及Rank()函数与row_number() 函数之间的区别。

本文用的是mysql 为8.0以上版本。

下面就用实例给大家演示一遍:

在Mysql数据库中创建一张雇员表employee:

  1. create table if not EXISTS `employee`
  2. ( `id`  int UNSIGNED AUTO_INCREMENT ,
  3.   `name`  varchar(20) not null,
  4.  `salary`  int not null ,
  5.  PRIMARY KEY(`id`)
  6.  )ENGINE=InnoDB DEFAULT CHARSET=utf8;

向employee表中插入数据:

  1. insert into employee (name,salary) VALUES ('张三',12000);
  2. insert into employee (name,salary) VALUES ('李四',12500);
  3. insert into employee (name,salary) VALUES ('王二麻子',15000);
  4. insert into employee (name,salary) VALUES ('赵信',13000);
  5. insert into employee (name,salary) VALUES ('关羽',12500);
  6. insert into employee (name,salary) VALUES ('张飞',13000);

查看创建的雇员表

select * from employee

分别用三种排序函数对salary字段进行排名:

  1. select name ,
  2. salary,rank() over ( ORDER BY salary desc) as rank1,
  3. ROW_NUMBER( )over ( ORDER BY salary desc) as ROW1,
  4. DENSE_RANK( ) over ( ORDER BY salary desc) as DENSE1
  5. from employee

结果如下:

综合上面的结果可以得出结论:

1、rank    遇重复值排序并列,然后跳跃到当前排序记录次数开始(递增或递减)排序

2、row_number  遇重复值排序不并列,连续不间断(递增或递减)排序

3、dense_rank  遇重复值排序并列,然后继续不间断(递增或递减)排序

以上就是Rank、row_number、dense_rank 三种排序函数的区别啦,小伙伴们最好自己动手操作一遍,这样印象更深刻。

 

 

 

标签:salary,dense,name,number,Rank,rank,employee,排序,row
From: https://www.cnblogs.com/yclizq/p/16923031.html

相关文章

  • HDU3652 B-number
    Idea数位DP.注意加一点记录.第一次写的时候把\(i\)写成了1,调整了好久,第二个错误是没注意到13是要连续的.因此Code可以再简化.Code#include<bits/stdc++.h>usin......
  • 200. Number of Islands
    Givenan mxn 2Dbinarygrid grid whichrepresentsamapof '1's(land)and '0's(water),return thenumberofislands.An island issurroundedbywa......
  • [LeetCode] 795. Number of Subarrays with Bounded Maximum
    Givenanintegerarray nums andtwointegers left and right,return thenumberofcontiguousnon-empty subarrays suchthatthevalueofthemaximumarr......
  • 动态规划- [USACO1.5][IOI1994]数字三角形 Number Triangles
    [USACO1.5][IOI1994]数字三角形NumberTriangles题目描述观察下面的数字金字塔。写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大。每一步可以......
  • [LeetCode] 2133. Check if Every Row and Column Contains All Numbers
    An nxn matrixis valid ifeveryrowandeverycolumncontains all theintegersfrom 1 to n (inclusive).Givenan nxn integermatrix matrix,re......
  • POJ2104-K-th Number(浅析主席树)
    K-thNumberTimeLimit: 20000MS MemoryLimit: 65536KTotalSubmissions: 65162 Accepted: 22979CaseTimeLimit: 2000MSDescriptionYouareworkingforMac......
  • JiLi Number
    ProblemK.JiLiNumberDescriptionDriverJilikesthedigit"1".Hehasanaccumulatorwhichshowsthesumofinputnumber.Helistsallofpositivenumbernomore......
  • poj 1423 Big Number<<求N!位数>>
    BigNumberTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions:27542Accepted:8789DescriptionInmanyapplicationsverylarg......
  • vue2 双向绑定3 v-model 及专用修饰符 .number .trizy .lazy
    v-model:在不操控dom的时候,快速获取表单内的数据,双向绑定,更改input框的时候,data值也会随之改变    修饰符:.number将输入值转为number类型......
  • 136. Single Number
    Givena non-empty arrayofintegers nums,everyelementappears twice exceptforone.Findthatsingleone.Youmust implementasolutionwithalinearr......