• Sqlserver:班级排名问题(转发)



    id,name(人名),class(班级),fen(分数)
    1  jj          1          88
    2  j1          1            90
    3  j2            2        70
    选出每个班级头两名分数, 显示班级,人名,分数????????


    create table #t (id int,name varchar(20),class varchar(10),fen int)
    insert into #t
    select 1,'jj','1',88 union all
    select 2,'j1','1',90 union all
    select 3,'j2','1',70 union all
    select 4,'j3','2',89 union all
    select 5,'ja','3',96 union all
    select 6,'jb','3',63

    解法一:
    select *
    from #t t
    where (select count(*) from #t where class="t".class and fen > t.fen) =0 -- 第一名
    where (select count(*) from #t where class="t".class and fen > t.fen) =1 -- 第二名
    where (select count(*) from #t where class="t".class and fen > t.fen) <2 -- 前2名
    where (select count(*) from #t where class="t".class and fen < t.fen) =0 -- 倒数第一名
    where (select count(*) from #t where class="t".class and fen < t.fen) =1 -- 倒数第二名
    where (select count(*) from #t where class="t".class and fen < t.fen) <2 -- 倒数前2名

    解法二:
    select t.* from #t t where fen in (select top 2 fen from #t where class = t.class) order by class,fen

    解法三:
    select *
    from
    (
    select row_number() over(partition by  class order by fen desc) as rank,* from #t
    ) t
    where rank <=2
    order by class,rank,fen
    附注:排名函数
    rank():稀疏排名,排名可能会并列,因此引起排名的数字间断。如有两个并列第一,则接下来的排名为第三!
    dense_rank():非稀疏排名,排名可能会并列,但排名的数字不会间断。如有两个并列第一,则接下来的排名为第二!
    ntile(group count):(抽屉分配)将数据按分组数分组 分配。主要是看总行数 是否 能被 组数 平均分配,如果不能平均分配,则会将数据有限分配到前面的组中!
    row_number():分配唯一的逻辑行记录的id,通常用于分页用 
    over子句还支持聚合函数 和 窗口分区子句

    解法四:
    select distinct t.*
    from #t a
    cross apply (select top 2 * from #t b where a.class = b.class order by fen desc) as t --内部(左值)多值交叉表
    order by t.class,t.fen desc


    解法五:
    select distinct t.*
    from #t a
    outer apply (select top 2 * from #t b where a.class = b.class order by fen desc) as t --外部(右值)多值交叉表
    order by t.class,t.fen desc

  • 相关阅读:
    node中一个基本的HTTP客户端向本地的HTTP服务器发送数据
    HTTP客户端之使用request方法向其他网站请求数据
    node的close
    node的超时timeout
    node中的ajax提交小例子
    node中转换URL字符串与查询字符串
    node.js 获取客户端信息
    用SCMD2.0.8.0汉化版制作OB地图简易教程
    js 调用 android 安卓 代码
    项目需要简单些了个WEB APP 的弹出窗
  • 原文地址:https://www.cnblogs.com/jinzhenshui/p/1621975.html
Copyright © 2020-2023  润新知