Skip to content

Instantly share code, notes, and snippets.

@lil12t
Last active December 31, 2020 11:05
Show Gist options
  • Save lil12t/2df9438d5b19417534b9ee144c5d14b4 to your computer and use it in GitHub Desktop.
Save lil12t/2df9438d5b19417534b9ee144c5d14b4 to your computer and use it in GitHub Desktop.
Select latest items grouped by status (non duplicates)
/* OUTPUTS ALL STATUSES WITH LATEST ITEM, 4 STATUSES = 4 ITEMS IN OUTPUT */
SELECT m1.*
FROM stats m1
LEFT JOIN stats m2
ON (m1.status = m2.status AND m1.created_at < m2.created_at)
WHERE m2.id IS NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment