62,052
社区成员
发帖
与我相关
我的任务
分享
if exists (select * from dbo.sysobjects where id = object_id('tb'))
drop table tb
go
Create table tb (ID int,Tickets int)
insert into tb
select 1,10 union all
select 2,15 union all
select 3,12 union all
select 4,12 union all
select 5,13
go
SELECT *,排序=RANK() OVER (ORDER BY Tickets DESC)
FROM Tb
/* 执行结果
ID Tickets 排序
----------- ----------- --------------------
2 15 1
5 13 2
3 12 3
4 12 3
1 10 5
*/
--or:
--> 测试数据: tb
if object_id('tb') is not null drop table tb
create table tb (ID int,Tickets int)
insert into tb
select 1,10 union all
select 2,15 union all
select 3,12 union all
select 4,12
go
declare @id int
set @Id=3
select * from
(select *,排名=DENSE_RANK() over(order by tickets desc) from tb)a where id=@id