how to sort by timestamp field while counting duplicates I have a table with username, timestamp and action columns. I need to retrieve a list of usernames, total occurrences of each user and the latest timestamp for each user. The following statement works but it doesn't sort the timestamp field. I've tried adding "ORDER BY time DESC" but it has no effect. How can I retrieve the latest timestamp? The statement below returns the first occurrence of the timestamp.

user|time|action
jones|1-1-2016|actionA
jones|1-2-2016|actionA
jones|2-1-2016|actionB

SELECT user, time, COUNT(*) AS total 
FROM table 
GROUP BY user 
HAVING total &gt; 0


Expected Results:

jones|2-1-2016|3
Simply apply a MAX:

SELECT user, MAX(time), COUNT(*) AS total 
FROM table 
GROUP BY user 
ORDER BY MAX(time) DESC


Of course, this assumes that time is actually a datetime or timestampand not a VarChar.