Skip to content

Instantly share code, notes, and snippets.

@quidproquo
Last active June 15, 2023 14:22
Show Gist options
  • Save quidproquo/c0b45e77f059863ae449 to your computer and use it in GitHub Desktop.
Save quidproquo/c0b45e77f059863ae449 to your computer and use it in GitHub Desktop.
SQL for selecting row with max timestamp
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