Skip to content

Instantly share code, notes, and snippets.

@wujku
Last active February 18, 2021 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wujku/b1c033aeb8ddc415817d to your computer and use it in GitHub Desktop.
Save wujku/b1c033aeb8ddc415817d to your computer and use it in GitHub Desktop.
SELECT c.*, p1.*
FROM customer c
JOIN purchase p1 ON (c.id = p1.customer_id)
LEFT OUTER JOIN purchase p2 ON (c.id = p2.customer_id AND
(p1.date < p2.date OR p1.date = p2.date AND p1.id < p2.id))
WHERE p2.id IS NULL;
@wujku
Copy link
Author

wujku commented May 22, 2018

Second example:

select pr.* from privacy_regulations as pr 
left outer join privacy_regulations as pr1 on (pr1.type = pr.type and pr1.version > pr.version)
where pr1.version is null
group by pr.type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment