SQL分组求每组最大值问题的解决方法收集 (转载)
2017-02-20 11:12
429 查看
例如有一个表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 )
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 )
相关文章推荐
- SQL分组求每组最大值问题的解决方法收集
- SQL常见问题解决方法收集
- 有关用virtual pc 安装Redhat的问题的解决方法(转载)
- [Lucene.Net] 内存泄漏问题解决方法 (转载)
- 对于重装SQL 2000出现的某些挂起文件问题解决方法
- Linq to SQL Delete时遇到问题的解决方法
- jbuilderx 光标定位问题解决方法(转载)
- Apache 2乱码问题解决方法--转载中文php网
- SQL Timeout 的问题之解决方法
- 关于SQL的安全问题(ftp.exe、cmd.exe的解决方法)
- asp读sql数据时出现乱码问题的解决方法
- (转载)Java中文问题及最优解决方法
- 中文乱码问题的解决方法 转载
- Java使用Oracle遇到的最大游标超出问题及其解决方法
- (转载)Java中文问题及最优解决方法
- SQL安装中常出现的问题与解决方法
- 关于SQL安装时出现的有程序安装挂起问题的解决方法
- 在从池中获取连接之前超时时间已过,所有池连接都已被使用并已达到最大池大小的问题解决方法
- ArcSDE 安装过程出现问题以及解决方法(转载,收藏版)
- SQL Timeout 的问题之解决方法