读书人

怎样知道一条数据在不同排序方法下的排

发布时间: 2012-04-01 17:23:46 作者: rapoo

怎样知道一条数据在不同排序方法下的排名?
如题

[解决办法]
增加一个自增列查看排名
[解决办法]
select num = (select count(1) form table a where a.排序字段 < 排序字段 order by a.排序字段 ) from table
[解决办法]
create table test(id int,age int)
insert test select 1,20
union all select 2,36
union all select 4,10
union all select 10,25
union all select 11,23
union all select 6,38

select bh=identity(int,1,1),* into # from test

select bh from # where id=10
order by age

drop table test,#

bh
-----------
4
[解决办法]
select id,[排名]=(select count(1) from [user] where [user].[年龄] <=a.[年龄]) from [user] a where id=10

读书人网 >SQL Server

热点推荐