给定这样的表,每个监视器的最新校准信息将是什么查询?换句话说,我想找到每个监视器的最大日期值。特定于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)