8

我想对表中的行进行排名而不跳过排名中的数字。请看下面的例子。

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<--

我不必使用 Rank 函数,只要我得到所需的排序即可。

谢谢!!

4

1 回答 1

20

尝试使用DENSE_RANK而不是 RANK

select *, DENSE_RANK() over (order by apples) as theRank from #test
于 2012-04-05T23:53:23.497 回答