Friday, February 24, 2012

help with limiting query results

Hi,

I have the following table in postgres:

hostname | username | logontime
----+-----+---------
ws1 | rautaonn | 2004-01-13 21:25:01.100336
ws1 | administrator | 2004-01-13 21:25:07.706546
ws1 | testuser | 2004-01-13 21:25:16.084844
ws2 | testuser | 2004-01-13 21:25:18.683653
ws2 | testuser2 | 2004-01-13 21:25:20.862199
ws2 | administrator | 2004-01-13 21:25:25.932736
ws2 | oizone | 2004-01-13 21:25:30.107574

and I would need to create a query that selects each hostname only once with username that has the latest timestamp in the logontime column.
The real table has about 5000 rows with 500 different hostnames, and I would need this query for reporting. Any help would be appreciated.

Thank in advance.

-Onni Rautanenselect t.*
from table t
where logontime = (select max(logontime) from table where id = t.id);

No comments:

Post a Comment