Mawg
Mawg

Reputation: 40140

Selecting most recent row, per user

I have a table with columns user_id, time_stamp and activity which I use for recoding user actions for an audit trail.

Now, I would just like to get the most recent timestamp for each unique user_id. How do I do that?

Upvotes: 3

Views: 1737

Answers (2)

Oliver M Grech
Oliver M Grech

Reputation: 3171

The following query should be what you want...

select user_id,max(time_stamp) from yourtable group by user_id order by user_id, time_stamp desc 

Upvotes: 3

AJ.
AJ.

Reputation: 28174

SELECT MAX(time_stamp), user_id FROM table GROUP BY user_id;

Upvotes: 7

Related Questions