加载中…
个人资料
凡不庸
凡不庸
  • 博客等级:
  • 博客积分:0
  • 博客访问:256,513
  • 关注人气:12
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

SQL分组求每组最大值问题的解决方法收集

(2011-02-10 13:06:13)
标签:

杂谈

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

id      name     sort      score

       张三      语文      82

       李四      数学       95

       王五      语文       88

       小东       英语       86

       张三      数学       92

       小红      体育       80

要求查询的结果如下:

id      name     sort      score

       王五      语文       88

       李四      数学       95

       小东       英语       86

       小红      体育       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 )

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有