大家好,我做过许多的 关于排序,以及查询个人排名的问题,
但是有的时候觉得自己还是做的不够好,所以想和大家讨论下。
把各自的使用方式说一下,分享一下。
比如按以下的表CS_Match_Stats
里面有很多字段其中
阵亡比:kills/deaths
爆头率:hits/shots
查询某玩家(uid)在某场(matchid)比赛中 的排名
1.查询个人的 field1 field2 (查询后为:2.667 ,1.0000)
select
Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths)) as field1 ,
Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots)) as field2
from CS_Match_Stats where matchid=3 and Uid=11
2.查出 比field1 大的数量
select COUNT(1)+1 from CS_Match_Stats
where matchid=3 and Deaths>0
and Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths))>2.667
3.查出 和field1相同,但比field2大的数量
select COUNT(1) from CS_Match_Stats
where
matchid=3 and Deaths>0 and Shots >0
and Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths))=2.667
and Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots))>1.0000
那么最后 2中和3中查询出来的数量相加 就是 该玩家的排名
当然这个比较麻烦, --with用法
with tb
as
(
select row_number() over(order by
Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths)) desc,
Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots)) desc
) as rankid,Uid
from CS_Match_Stats where matchid=3 and Deaths>0 and Shots >0
)
select * from tb where Uid=11
sqlserver 2005 版本以上 增加了很多 函数如:row_number() rank(),等一些函数,
然后使用with,或 select rankid from (select ………… from ^^^) where …………,
他们使用起来就比较简单,
如果你的数据量不大的话,
确实使用它们很不错,
可是如果 数据量很大的情况下,
使用它们就不是那么回事儿了 ,
所以,我想请大家给出些好的建议………………
但是有的时候觉得自己还是做的不够好,所以想和大家讨论下。
把各自的使用方式说一下,分享一下。
比如按以下的表CS_Match_Stats
里面有很多字段其中
阵亡比:kills/deaths
爆头率:hits/shots
查询某玩家(uid)在某场(matchid)比赛中 的排名
1.查询个人的 field1 field2 (查询后为:2.667 ,1.0000)
select
Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths)) as field1 ,
Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots)) as field2
from CS_Match_Stats where matchid=3 and Uid=11
2.查出 比field1 大的数量
select COUNT(1)+1 from CS_Match_Stats
where matchid=3 and Deaths>0
and Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths))>2.667
3.查出 和field1相同,但比field2大的数量
select COUNT(1) from CS_Match_Stats
where
matchid=3 and Deaths>0 and Shots >0
and Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths))=2.667
and Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots))>1.0000
那么最后 2中和3中查询出来的数量相加 就是 该玩家的排名
当然这个比较麻烦, --with用法
with tb
as
(
select row_number() over(order by
Convert(numeric(18,4),convert(numeric(18,4),Kills)/convert(numeric(18,4),Deaths)) desc,
Convert(numeric(18,4),convert(numeric(18,4),Hits)/convert(numeric(18,4),Shots)) desc
) as rankid,Uid
from CS_Match_Stats where matchid=3 and Deaths>0 and Shots >0
)
select * from tb where Uid=11
sqlserver 2005 版本以上 增加了很多 函数如:row_number() rank(),等一些函数,
然后使用with,或 select rankid from (select ………… from ^^^) where …………,
他们使用起来就比较简单,
如果你的数据量不大的话,
确实使用它们很不错,
可是如果 数据量很大的情况下,
使用它们就不是那么回事儿了 ,
所以,我想请大家给出些好的建议………………
只是想 寻求最佳解决方案,大家咋都这么势力呢,
为了一点分伤了感情多不好啥。