Given a TASK_ID, what is the most efficient way to retrieve the latest version of each file?
Here is an example table I am working with.
ID TASK_ID FILENAME VERSION_NUMBER RANDOM_COLUMN
1 1 a.txt 1 112
2 1 b.txt 1 231231
3 1 c.txt 1 234356
4 1 a.txt 2 35
5 1 b.txt 2 84
6 1 b.txt 3 97604
7 2 aa.txt 1 6956
8 2 bb.txt 1 9760054
This is what I have so far.
SELECT FILENAME, TASK_ID, MAX(VERSION_NUMBER)
FROM TABLE_NAME
GROUP BY FILENAME, TASK_ID
HAVING TASK_ID = '1';
Unfortunately, the above only gives me the 3 columns I have in the SELECT statement instead of the entire row and adding more columns will give me an error.
FILENAME TASK_ID MAX(VERSION_NUMBER)
a.txt 1 2
b.txt 1 3
c.txt 1 1
How might I go about getting the entire row?
I plan on adding this SQL query to a Query annotation within a Spring Repository and run it as a native query. Is that the best way?
EDIT: I ended up with this SQL query, which gets what I want, but I am not sure whether this is correct or how efficient it is.
SELECT ta.*
FROM TABLE_NAME ta
INNER JOIN (SELECT FILENAME, TASK_ID, MAX(VERSION_NUMBER) vn
FROM TABLE_NAME
GROUP BY FILENAME, TASK_ID
HAVING TASK_ID = '1') sq
ON ta.FILENAME = sq.FILENAME
AND ta.TASK_ID = sq.TASK_ID
AND ta.VERSION_NUMBER = sq.vn;
Result of above SQL query
ID TASK_ID FILENAME VERSION_NUMBER RANDOM_COLUMN
3 1 c.txt 1 234356
4 1 a.txt 2 35
6 1 b.txt 3 97604