如果实现查询排序后某个记录所在的记录行
比如:
name point
小王 30
小李 35
小陈 80
小刘 25
小张 15
要实现比如排序后小刘排在第2个记录,我查询小刘,返回下面值:
name point
小刘 25 2
请问要怎么实现?
问题点数:30、回复次数:9Top
1 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-09-22 16:08:56 得分 2
--测试环境
declare @t table(name varchar(10), poin int)
insert into @t select '小王',30
union all select '小李',35
union all select '小陈',80
union all select '小刘',25
union all select '小张',15
--查询
select *,名次=(select sum(1) from @t where poin>=a.poin)
from @t a
where
(select sum(1) from @t where poin>=a.poin)=2 --第二名
--结果
name poin 名次
---------- ----------- -----------
小李 35 2
(所影响的行数为 1 行)
Top
2 楼thordon(索尔的吼声)回复于 2005-09-22 16:09:59 得分 2
select identity(int,1,1) As id,* into #temp from t_ls order by userid desc
select * from #tempTop
3 楼vivianfdlpw()回复于 2005-09-22 16:10:00 得分 2
select identity(int,1,1) as ID,name,point into # from 表
select name,point,ID from # where name='小刘'Top
4 楼thordon(索尔的吼声)回复于 2005-09-22 16:10:29 得分 2
select identity(int,1,1) As id,* into #temp from t_ls order by userid desc
select * from #tempTop
5 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-09-22 16:13:07 得分 2
--如果Poin 有相同的分数排名如下:
--测试环境
declare @t table(name varchar(10), poin int)
insert into @t select '小王',30
union all select '小李',35
union all select '小陈',80
union all select '小刘',25
union all select '小张',15
union all select '小邹',15
--查询
select *,名次=(select count(distinct poin) from @t where poin>=a.poin)
from @t a
where
(select count(distinct poin) from @t where poin>=a.poin)=5 --第五名
--结果
name poin 名次
---------- ----------- -----------
小张 15 5
小邹 15 5
(所影响的行数为 2 行)
Top
6 楼MorningTea(一勺抹茶)回复于 2005-09-22 16:13:21 得分 10
declare @T table (name varchar(20),point int)
insert into @T
select '小王',30 union all
select '小李',35 union all
select '小陳',80 union all
select '小劉',25 union all
select '小張',15
select name,point,sn=(select count(1) + 1 from @T T2 where T2.point < T1.point)
from @T T1
where name = '小劉'Top
7 楼lizhaogui()回复于 2005-09-22 16:13:41 得分 2
select name ,ponit ,count(*)+1-(select count(*) from table a where point>(select point from table b where name='小刘 ') as hao
from table where name='小刘 ' order by ponit;Top
8 楼MorningTea(一勺抹茶)回复于 2005-09-22 16:14:28 得分 8
declare @T table (name varchar(20),point int)
insert into @T
select '小王',30 union all
select '小李',35 union all
select '小陳',80 union all
select '小劉',25 union all
select '小張',15
select name,point,sn=(select count(1) + 1 from @T T2 where T2.point < T1.point)
from @T T1
where name = '小劉'
/*
(5 row(s) affected)
name point sn
-------------------- ----------- -----------
小劉 25 2
(1 row(s) affected)
*/Top
9 楼newmcz(newmcz)回复于 2005-09-22 16:23:06 得分 0
markTop





