问个排名的问题

noyester 2005-08-13 05:18:43
一个字段中,例如2,6,9,8,55,88,522

怎么写SQL能得到8的排名?从大到小排,应该是第4,怎么写呢?突然想不出来了
...全文
168 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
noyester 2005-08-14
  • 打赏
  • 举报
回复
10万个用户排名,会不会效率太低了?

有好的方法吗?能不能每天自动排名一次?
点点星灯 2005-08-14
  • 打赏
  • 举报
回复
--测试数据了
declare @tab table(num int)
insert @tab values(2)
insert @tab values(6)
insert @tab values(9)
insert @tab values(8)
insert @tab values(55)
insert @tab values(88)
insert @tab values(522)

--这才是你要的方法了.
select * ,pm=(Select IsNull(Count(Distinct num),0)+1 from @tab Where num>A.num)
from @tab A
order by pm
noyester 2005-08-14
  • 打赏
  • 举报
回复
SQ俺菜的很,索引还没用过呢,算了不排名了。
点点星灯 2005-08-14
  • 打赏
  • 举报
回复
那就建索引了.
noyester 2005-08-13
  • 打赏
  • 举报
回复
declare @tab table(num int)
insert @tab values(2)
insert @tab values(6)
insert @tab values(9)
insert @tab values(8)
insert @tab values(55)
insert @tab values(88)
insert @tab values(522)
--select * from @tab
select * ,pm=(Select IsNull(Count(Distinct num),0)+1 from @tab Where num>A.num)
from @tab A
order by pm


加入有10万条数据, insert 10万次?
noyester 2005-08-13
  • 打赏
  • 举报
回复
下班了,回家看看,这就是最简单的方法吗?
点点星灯 2005-08-13
  • 打赏
  • 举报
回复

num pm
----------- -----------
522 1
88 2
55 3
9 4
8 5
6 6
2 7

(所影响的行数为 7 行)
点点星灯 2005-08-13
  • 打赏
  • 举报
回复
declare @tab table(num int)
insert @tab values(2)
insert @tab values(6)
insert @tab values(9)
insert @tab values(8)
insert @tab values(55)
insert @tab values(88)
insert @tab values(522)
--select * from @tab
select * ,pm=(Select IsNull(Count(Distinct num),0)+1 from @tab Where num>A.num)
from @tab A
order by pm

hsj20041004 2005-08-13
  • 打赏
  • 举报
回复
2,6,9,8,55,88,522
从大到小排,应该是第5吧
create table #t(ii int)
insert into #t values( 2)
insert into #t values (6)
insert into #t values (9)
insert into #t values (8)
insert into #t values (55)
insert into #t values (88)
insert into #t values( 522)

select count(*)+1 from #t where ii>8
noyester 2005-08-13
  • 打赏
  • 举报
回复
SELECT Point, UserName
FROM T_UserInfo
WHERE (UserName = '农民')


Point是积分,UserName是用户名,怎么得到该用户的积分排第几呢?
filebat 2005-08-13
  • 打赏
  • 举报
回复
将该表与一个有序号列的表进行一下表连接,从而实现行列转化.
或者通过字符串函数(chaindex, substring等).

前一种方法简单一些, 后一种直观,但是繁琐.

楼主给出具体表结构和数据先啊.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧