CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
【经验总结】不能实施并行处理的情况 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

如果实现查询排序后某个记录所在的记录行

楼主crazy_baby(危险的屁)2005-09-22 16:03:05 在 MS-SQL Server / 基础类 提问

比如:  
  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

相关问题

  • 请问ejb中查询记录的传递和排序问题??
  • 查询记录的所在行数?
  • 当查询记录为空记录集时,查询语句中的定义排序问题
  • 如何在adoQuery中增加一个计算字段,来显示查询出的记录的排序的号?记录一显示1,记录二显示2....
  • 如何给记录排序?
  • 记录排序问题
  • listview记录排序问题?
  • 这个查询排序语句怎么写呀`?以id排序。读取库里删除字段为false的所有记录~!
  • ADO查询?或是排序?
  • 两个查询结果怎么用Sql处理记录集相加,不能用union all,因为一个查询乐有排序方式,另外一个没有

关键词

  • poin
  • allselect
  • union
  • 刘
  • 实现
  • point
  • identity
  • 王
  • 李
  • declare

得分解答快速导航

  • 帖主:crazy_baby
  • zlp321002
  • thordon
  • vivianfdlpw
  • thordon
  • zlp321002
  • MorningTea
  • lizhaogui
  • MorningTea

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
CSDN网站24小时值班电话:13552009689
Copyright © 2000-2009, CSDN.NET, All Rights Reserved
GongshangLogo