CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

求一统计表(高分回报)

楼主splory(爽儿)2006-02-25 15:28:53 在 MS-SQL Server / 基础类 提问

有如下一张表:  
  排名           stuname     stuid       classid         总分  
  4 张三 101 1 504.0  
  6 李四 102 1 485.0  
  2 王五 103 1 530.0  
  1 小白 104 1 544.0  
  3 宋华 105 1 523.0  
  5 钱一 106 1 486.0  
  1 赵善 201 2 558.0  
  2                 陈锋           202             2                   521.0  
   
  对这张表做查询,使得得到的结果为:(就是要去除班级的前十名单和年级的前十名单)  
  (班级是动态的哈)  
  名次             1班                                         2班                                     年级  
  第一名       小白(544.0)                       赵善(558.0)                       赵善(2班)  
  第二名       王五(530.0)                       陈锋(521.0)                       小白(1班)  
   
   
  谢谢给为老大们先  
  最近搞一关于这方面得咚咚,把我快搞挂了  
  代码共享,谢谢啦 问题点数:100、回复次数:9Top

1 楼splory(爽儿)回复于 2006-02-25 15:37:35 得分 0

正确的:  
  对这张表做查询,使得得到的结果为:(就是求班级的前十名单和年级的前十名单)  
  (班级是动态的哈)  
  Top

2 楼splory(爽儿)回复于 2006-02-25 15:38:00 得分 0

写错了Top

3 楼splory(爽儿)回复于 2006-02-25 17:25:23 得分 0

高手快来给个帮手阿  
  谢谢啦Top

4 楼rouqu(石林#黄果树)回复于 2006-02-25 18:18:06 得分 0

我自己最差的就是写复杂的SQL。。。Top

5 楼rouqu(石林#黄果树)回复于 2006-02-25 18:21:48 得分 0

你原始表如果有很多个班级怎么办?Top

6 楼wgsasd311(自强不息)回复于 2006-02-25 18:47:25 得分 0

 
  select   名次,  
  [1班]=max(case   flag   when   1   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   ),  
  [2班]=max(case   flag   when   2   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   ),  
  [年级]=max(case   flag   when   0   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   )   from  
  (select   top   10   名次=(select   count(1)   from   tb   where   总分>=a.总分   ),stuname,总分,0   as   flag  
  from   tb   a    
  union   all  
  select   top   10     名次=(select   count(1)   from   tb   where   总分>=a.总分   and   classid=a.classid   ),stuname,总分,1  
  from   tb   a   where   classid=1  
  union   all  
  select   top   10     名次=(select   count(1)   from   tb   where   总分>=a.总分   and   classid=a.classid   ),stuname,总分,2  
  from   tb   a   where   classid=2)aa  
  group   by   名次   order   by   名次Top

7 楼wgsasd311(自强不息)回复于 2006-02-25 19:21:17 得分 100

 
  create   table   tb(排名   int,stuname   varchar(10),stuid   int,classid   int   ,总分   decimal(9,2))  
  insert   into   tb  
  select   4,'张三',101,1,504.0   union   all  
  select   6,'李四',102,1,485.0   union   all  
  select   2,'王五',103,1,530.0   union   all  
  select   1,'小白',104,1,544.0   union   all  
  select   3,'宋华',105,1,523.0   union   all  
  select   5,'钱一',106,1,486.0   union   all  
  select   1,'赵善',201,2,558.0   union   all  
  select   2,'陈锋',202,2,521.0  
  go  
   
  declare   @s   varchar(8000),@s2   varchar(8000)  
  set   @s='select   a.*,b.年级   from   (select   top   10   [名次]=排名'  
  select   @s=@s+',['+cast(classid   as   varchar)+'班]=max(case   classid   when   '+cast(classid   as   varchar)  
  +   '   then   stuname   +   ''(''+cast(总分   as   varchar)+'')''   else   ''''   end)'  
    from   tb   group   by   classid  
  --print   @s  
  set   @s=@s+'   from   tb   group   by   排名     )a   '  
  set   @s2='   left   join   (select   top   10   名次=(select   count(1)   from   tb   where   总分>=a.总分   ),  
  年级=stuname+''(''+cast(总分   as   varchar)+'')''   from   tb   a   )   b   on   a.名次=b.名次   order   by   a.名次'  
  exec(@s+@s2)  
  go  
  drop   table   tb  
  goTop

8 楼wgsasd311(自强不息)回复于 2006-02-25 19:22:32 得分 0

 
  create   table   tb(排名   int,stuname   varchar(10),stuid   int,classid   int   ,总分   decimal(9,2))  
  insert   into   tb  
  select   4,'张三',101,1,504.0   union   all  
  select   6,'李四',102,1,485.0   union   all  
  select   2,'王五',103,1,530.0   union   all  
  select   1,'小白',104,1,544.0   union   all  
  select   3,'宋华',105,1,523.0   union   all  
  select   5,'钱一',106,1,486.0   union   all  
  select   1,'赵善',201,2,558.0   union   all  
  select   2,'陈锋',202,2,521.0  
  go  
   
  select   名次,  
  [1班]=max(case   flag   when   1   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   ),  
  [2班]=max(case   flag   when   2   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   ),  
  [年级]=max(case   flag   when   0   then   stuname+'('+cast(总分   as   varchar)+')'   else   ''   end   )   from  
  (select   top   10   名次=(select   count(1)   from   tb   where   总分>=a.总分   ),stuname,总分,0   as   flag  
  from   tb   a    
  union   all  
  select   top   10     名次=(select   count(1)   from   tb   where   总分>=a.总分   and   classid=a.classid   ),stuname,总分,1  
  from   tb   a   where   classid=1  
  union   all  
  select   top   10     名次=(select   count(1)   from   tb   where   总分>=a.总分   and   classid=a.classid   ),stuname,总分,2  
  from   tb   a   where   classid=2)aa  
  group   by   名次   order   by   名次  
  go  
  drop   table   tb  
  go  
   
  Top

9 楼zjcxc(邹建)回复于 2006-02-25 20:37:31 得分 0

--   sql   2005中的处理方法  
   
  create   table   tb(排名   int,stuname   varchar(10),stuid   int,classid   int   ,总分   decimal(9,2))  
  insert   into   tb  
  select   4,'张三',101,1,504.0   union   all  
  select   6,'李四',102,1,485.0   union   all  
  select   2,'王五',103,1,530.0   union   all  
  select   1,'小白',104,1,544.0   union   all  
  select   3,'宋华',105,1,523.0   union   all  
  select   5,'钱一',106,1,486.0   union   all  
  select   1,'赵善',201,2,558.0   union   all  
  select   2,'陈锋',202,2,521.0  
  go  
   
  declare   @s   varchar(max)  
  set   @s=''  
  select   @s=@s+','+quotename(classid)  
  from   tb  
  group   by   classid  
  order   by   classid  
  set   @s=stuff(@s,1,1,'')  
  exec('  
  select   a.*,b.stuname  
  from(  
  select   *  
  from(  
  select    
  stuname   =   stuname   +   quotename(总分,   ''()''),   classid,  
  排名  
  from   tb  
  where   排名<=10  
  )data  
  pivot(  
  max(stuname)  
  for   classid   in('+@s+')  
  )p  
  )a,(  
  select   top   10   stuname,   排名  
  from(  
  select    
  stuname   =   stuname   +   quotename(classid,   ''()''),   classid,  
  排名=row_number()   over(order   by   总分   desc)  
  from   tb  
  )data  
  )b   where   a.排名=b.排名')  
  go  
   
  drop   table   tb  
   
  --   结果  
   
  排名                     1                     2                         stuname  
  -----------   ------------   ------------   -----------  
  1                       小白(544.00)   赵善(558.00)   赵善(2)  
  2                       王五(530.00)   陈锋(521.00)   小白(1)  
  3                       宋华(523.00)   NULL                   王五(1)  
  4                       张三(504.00)   NULL                   宋华(1)  
  5                       钱一(486.00)   NULL                   陈锋(2)  
  6                       李四(485.00)   NULL                   张三(1)  
   
  (6   row(s)   affected)  
   
  Top

相关问题

  • 高分急求一个统计表的查询
  • 统计表中记录数(50分)
  • 做过统计表的高手们,帮忙了!如若能实现,送出我的全部分!
  • 两张统计表的相加,看看。送分
  • 怎么生成交叉统计表??新手问题,100分
  • 统计表
  • 急需!高分回报
  • 统计表的空间
  • 如何生成统计表?
  • 100分 请问有没有可以用来做统计表的控件,或是统计学专用函数什么的,谢谢了.

关键词

  • 总分
  • top
  • stuname
  • 名次
  • 班级
  • classid
  • 年级
  • union allselect
  • 表
  • 谢谢

得分解答快速导航

  • 帖主:splory
  • wgsasd311

相关链接

  • SQL Server类图书

广告也精彩

反馈

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