使用 SQL Server Rank 函数对行进行排名而不跳过排名号
Posted
技术标签:
【中文标题】使用 SQL Server Rank 函数对行进行排名而不跳过排名号【英文标题】:Ranking rows using SQL Server Rank function without skipping a rank number 【发布时间】:2012-04-19 17:20:15 【问题描述】:我想对表格中的行进行排名而不跳过排名中的数字。请看下面的例子。
CREATE TABLE #test(
apples int NOT NULL,
) ON [PRIMARY]
GO
insert into #test( apples ) values ( 10 )
insert into #test( apples ) values ( 10 )
insert into #test( apples ) values ( 20 )
insert into #test( apples ) values ( 30 )
select *, RANK() over (order by apples) as theRank from #test
drop table #test
go
结果是
apples theRank
10 1
10 1
20 3
30 4
如何让排名不跳过数字 2,让结果看起来像
apples theRank
10 1
10 1
20 2<--
30 3<--
我不必使用排名功能,只要我得到所需的排序。
谢谢!!
【问题讨论】:
【参考方案1】:尝试使用DENSE_RANK 代替排名
select *, DENSE_RANK() over (order by apples) as theRank from #test
【讨论】:
对 T 甚至拼写和标点符号的答案完全相同。我会删除我的答案,因为它看起来像你的复制和粘贴,你比我早了 2 分钟。以上是关于使用 SQL Server Rank 函数对行进行排名而不跳过排名号的主要内容,如果未能解决你的问题,请参考以下文章
SQL SERVER 的窗体函数OVER的使用:row_number/rank/dense_rank
根据 SQL Server 2008 R2 中特定列中的模式更改对行进行分组
如何在 Sql Server Compact Edition SELECT 语句中复制 Rank 函数?
在 SQL Server 中使用 Dense_Rank 对具有排名的列进行排名组合