Table:
UserId, Value, Date.
I want to get the UserId, Value for the max(Date) for each UserId. That is, the Value for each UserId that has the latest date. Is there a way to do this simply in SQL? (Preferably Oracle)
Update: Apologies for any ambiguity: I need to get ALL the UserIds. But for each UserId, only that row where that user has the latest date.
This will also take care of duplicates (return one row for each user_id):
Solution for MySQL which doesn't have concepts of partition KEEP, DENSE_RANK.
Reference: http://benincampus.blogspot.com/2013/08/select-rows-which-have-maxmin-value-in.html
I know you asked for Oracle, but in SQL 2005 we now use this:
(T-SQL) First get all the users and their maxdate. Join with the table to find the corresponding values for the users on the maxdates.
results:
Just tested this and it seems to work on a logging table