SQL Server排序函数row_number和rank的区别

时间:2021-09-26 23:22:30

SQL Server排序函数row_number和rank的区别

直接看测试结果

declare @table table(name varchar(100),amount int, memo varchar(10))
insert into @table(name,amount,memo)
values('apple',8,'123')
,('apple',10,'123')
,('apple',12,'235')
,('orange',11,'123')
,('orange',14,'123')
,('pear',9,'123')

row_number

select row_number()over(order by name) as rn,* from @table

SQL Server排序函数row_number和rank的区别

rank

select rank()over(order by name) as rn,* from @table

SQL Server排序函数row_number和rank的区别