zoukankan      html  css  js  c++  java
  • SQL分组求每组最大值问题的解决方法收集 (转载)

    例如有一个表student,其结构如下:

    id      name     sort      score

    1        张三      语文      82

    2        李四      数学       95

    3        王五      语文       88

    4        小东       英语       86

    5        张三      数学       92

    6        小红      体育       80

    要求查询的结果如下:

    id      name     sort      score

    3        王五      语文       88

    2        李四      数学       95

    4        小东       英语       86

    6        小红      体育       80

    顺序可调换,即为每个科目的最高分信息

    SQL如下:

    法一:

    select student.id,student.name,student.sort,student.score from student inner join (select sort, max(score) as score from student group by sort) B on student.sort=B.sort AND student.score=B.score order by id

    法二:

    select * from student a where not exists(select * from student where a.score<score and a.sort=sort )

    法三:

    select * from student a where 1〉(select count(*) from student where a.score<score and a.sort=sort )

  • 相关阅读:
    python中几种数据类型常用的方法
    WSGI
    从开学到初赛的一些个人总结
    CSP-S2020 浙江 游记
    CF1416D Graph and Queries
    单次期望 O(1) 的RMQ
    P3177 [HAOI2015]树上染色
    CF835F Roads in the Kingdom/P1399 [NOI2013]快餐店
    P4381 [IOI2008]Island
    P5655 基础数论函数练习题
  • 原文地址:https://www.cnblogs.com/wjwen/p/6418670.html
Copyright © 2011-2022 走看看