I have a table where I store multiple observations over time of social media posts on a set of web pages. The table columns are page_id, post_id, and obs_date [plus others with my interesting data]. I want to query this table for the latest observation of each post_id for a given page_id. In SQL this would be something like
SELECT * FROM my_table
JOIN
(SELECT max(obs_date) AS obs_date, post_id FROM my_table
WHERE page_id='12345' GROUP BY post_id) AS t
ON t.obs_date=my_table.obs_date AND t.post_id=my_table.post_id
I have seen recipes for getting the latest item for a hash key in questions like this, but here I am wondering if it possible to take this one step further and get the latest item for a given hash key, and then for each of another attribute a la 'GROUP BY'. How would I set up my table(s) and keys to do this? Could I use Local or Global Secondary Indexes?
Any help or suggestion is greatly appreciated.