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 > 0
Expected Results:
jones|2-1-2016|3
Best Answer
Simply apply a
MAX
:Of course, this assumes that
time
is actually adatetime
ortimestamp
and not aVarChar
.