Skip to content

Instantly share code, notes, and snippets.

@slattery
Created December 12, 2014 03:47
Show Gist options
  • Save slattery/08d1d70dbe87fb88e93e to your computer and use it in GitHub Desktop.
Save slattery/08d1d70dbe87fb88e93e to your computer and use it in GitHub Desktop.
get first in each category in PostgreSQL
SELECT DISTINCT ON (message_id) message_id, date_trunc('day', load_time) as load_time
FROM www_acp.outgoing_messages
WHERE message_id IN (select document_id from documents where name ilike '%whatevz%')
ORDER BY message_id, load_time
-- this acts sort of like a group by with min( timestamp )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment