I have a table with three columns:
id | created_at | original_id
a1 | 2019-12-10 | a
a2 | 2019-12-12 | a
a3 | 2019-12-11 | a
b1 | 2019-12-10 | b
b2 | 2019-12-09 | b
I want to find the ids of all rows that have the largest created_at within all rows with the same original_id
(Background: the table stores the "history" of an element, so each time it is changed or when it is deleted, a new history-entry is created. I want to know the ids of the newest history-entry for each element/for each original_id)
So what I would expect as a result for the above example data would be:
id | MAX(created_at) | original_id
a2 | 2019-12-12 | a
b1 | 2019-12-10 | b
I have tried several ways of using subqueries and group-by but I can't really get the max value inside a grouped by
SELECT a.id, a.original_id, a.created_at FROM notes.test AS a
WHERE a.created_at = (SELECT MAX(b.created_at)
FROM notes.test AS b
WHERE b.original_id = a.original_id)
After I figure out how to even write this query with sql I also need to use it with a Java-Spring-Repository so any ideas on how to implement it there are also welcome