sql - sql max 函数用法
问题描述
例如,我的表中有一个值,如下所示
Id ExamType Value Date
1 IELTS 240 2019-01-01
2 IELTS 120 2018-12-01
3 TOELF 100 2017-12-12
4 TOELF 80 2019-01-20
5 GMAT 70 2016-01-01
我希望我的查询给我这些考试的最大值,如下所示
IELTS (2019) 240
TOELF (2017) 100
GMAT (2016) 70
我写了我的查询如下
SELECT x.ExamType,MAX(x.Value)
FROM TableExam x
GROUP BY x.ExamType
但是这个查询没有给出我想要的结果,所以我需要一个查询来得到上面的结果,谢谢你的帮助。
解决方案
查看您的示例似乎您需要加入最大值组 ExampType 的子查询
select m.ExamType, year(m.date), t.max_value value
from TableExam m
INNER JOIN (
select ExamType, max(value) max_value
from TableExam
group by ExampType
) t on t.max_value = m.value and t.ExamType = m.ExamType
推荐阅读
- java - 为什么我从数组 B 得到 0,但它在数组 A 上工作
- android - 是否可以通过 USB 集线器在两部安卓手机之间进行通信?
- d3.js - 更新 D3.js 树形图的数据
- c# - 使用 Entity Framework Core 进行表拆分。将表展平为主-详细信息
- python - python中的独立时间倒计时
- spring - Spring - 不持久记住我在应用程序重启时无法生存
- java - 如何从对象的 Arraylist 中获取子列表?
- macos - macOS High Sierra 10.13.6 上的一般编译问题
- php - 发现重复键时,WordPress会在日志中隐藏错误
- sql - Oracle SQL中与空字符串连接和强制转换函数有什么区别