The solution of window functions should be the most efficient and lead to only one scan of a table or index. The one I post here, I think, wins some points to be intuitive and understandable. I tested on the SQL server and performed the functions of the 2nd window, which led to two index scans.
SELECT T1.monitor_id, T1.calibration_date, T1.value FROM someTable AS T1 WHERE NOT EXISTS ( SELECT * FROM someTable AS T2 WHERE T2.monitor_id = T1.monitor_id AND T2.value > T1.value ) GROUP BY T1.monitor_id, T1.calibration_date, T1.value
And just for this, here is another one along the same lines, but less effective (63% against 37%) than the other (again in the sql server). In this case, the "Left outer join" is used in the execution plan, where the "Anti-merge" union is used as the first:
SELECT T1.monitor_id, T1.calibration_date, T1.value FROM someTable AS T1 LEFT JOIN someTable AS T2 ON T2.monitor_id = T1.monitor_id AND T2.value > T1.value WHERE T2.monitor_id IS NULL GROUP BY T1.monitor_id, T1.calibration_date, T1.value
J cooper
source share