I've tried a lot of the answers given on stackoverflow, but they're all very poor performance. They all suggest either using subqueries or joins with the same table where the join is on p1.pid = p2.pid AND p1.entryTime < p2.entryTime
. Both are very slow options.
Is there a performant way to query this?
DATA
PID STATUS ENTRYTIME
--- ------ ---------
001 start 2016-05-26 01:18:00
002 start 2016-05-26 01:19:00
001 done 2016-05-26 02:11:00
003 start 2016-05-26 11:12:00
002 done 2016-05-26 11:19:00
003 inprog 2016-05-26 12:15:00
WHAT SHOULD BE RETURNED
PID STATUS ENTRYTIME
--- ------ ---------
001 done 2016-05-26 02:11:00
002 done 2016-05-26 11:19:00
003 inprog 2016-05-26 12:15:00
I cannot figure out how to do this in one query that isn't extremely slow.
I've tried queries adapted form these answers:
Retrieving the last record in each group
Get records with max value for each group of grouped SQL results
https://stackoverflow.com/a/17038667/857025