gpt4 book ai didi

sql - Oracle:获得团队的最大值(value)?

转载 作者:行者123 更新时间:2023-12-03 02:22:06 24 4
gpt4 key购买 nike

给定一个这样的表,什么查询将获得每个显示器的最新校准信息?换句话说,我想找到每个监视器的最大日期值。 Oracle 特定的功能非常适合我的应用程序。

monitor_id     calibration_date  value
---------- ---------------- -----
1 2011/10/22 15
1 2012/01/01 16
1 2012/01/20 17
2 2011/10/22 18
2 2012/01/02 19

此示例的结果如下所示:

1  2012/01/20 17
2 2012/01/02 19

最佳答案

我倾向于使用分析函数

SELECT monitor_id,
host_name,
calibration_date,
value
FROM (SELECT b.monitor_id,
b.host_name,
a.calibration_date,
a.value,
rank() over (partition by b.monitor_id order by a.calibration_date desc) rnk
FROM table_name a,
table_name2 b
WHERE a.some_key = b.some_key)
WHERE rnk = 1

您也可以使用相关子查询,尽管效率较低

SELECT monitor_id,
calibration_date,
value
FROM table_name a
WHERE a.calibration_date = (SELECT MAX(b.calibration_date)
FROM table_name b
WHERE a.monitor_id = b.monitor_id)

关于sql - Oracle:获得团队的最大值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9220944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com