I have a log file for an application that shows two columns, username and timestamp. I'm trying to run a PostgreSQL query to show only the last time each user logged in.
This is the query I have put together so far:
SELECT DISTINCT username, MAX(timestamp) FROM log
GROUP BY username, timestamp;
The problem is the
SELECT username, MAX(timestamp) FROM log GROUP BY username;
However, if you want all the information from the
DISTINCT ON is a better choice:
SELECT DISTINCT ON (username) l.* FROM log ORDER BY username, timestamp DESC;
You use of
SELECT DISTINCT leads me to suspect that this might be what you want.