Skip to content

Instantly share code, notes, and snippets.

@dcrystalj
Created March 26, 2022 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcrystalj/c74c53d9f2621362a6641da4b8cab03a to your computer and use it in GitHub Desktop.
Save dcrystalj/c74c53d9f2621362a6641da4b8cab03a to your computer and use it in GitHub Desktop.
SELECT
"user".id,
"user".name,
(
SELECT l.name
FROM location l
WHERE location.user_id=l.user_id
ORDER BY l.moved_on DESC
LIMIT 1
) current_location,
CASE WHEN
EXISTS (
SELECT l.id
FROM location l
WHERE
location.user_id = l.user_id AND
location.moved_on > l.moved_on
)
THEN 'True'
ELSE 'False'
END AS is_miami_latest_location
FROM location
JOIN "user"
ON "user".id = location.user_id
WHERE
location.name = 'Miami'
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment