Last active
June 15, 2023 14:22
-
-
Save quidproquo/c0b45e77f059863ae449 to your computer and use it in GitHub Desktop.
SQL for selecting row with max timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT n.node_id, n.lat, n.lng, n.timestamp | |
FROM nodes AS n | |
INNER JOIN ( | |
SELECT node_id, MAX(timestamp) AS timestamp | |
FROM nodes | |
GROUP BY node_id | |
) AS mn | |
ON n.node_id = mn.node_id | |
AND n.timestamp = mn.timestamp; | |
DELETE n.* | |
FROM nodes AS n | |
INNER JOIN ( | |
SELECT node_id, MAX(timestamp) AS timestamp | |
FROM nodes | |
GROUP BY node_id | |
) AS mn | |
ON n.node_id = mn.node_id | |
AND n.timestamp < (mn.timestamp - 30 seconds) -- Verify the subtration operation you need, I'm not sure what it is offhand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment