【发布时间】:2010-11-11 12:32:14
【问题描述】:
我正在尝试从一个表中获取 ip、用户和最近的时间戳,该表可能同时包含用户的当前 ip 和一个或多个先前的 ip。我想为每个用户添加一行,其中包含最新的 ip 和相关的时间戳。所以如果一个表是这样的:
username | ip | time_stamp
--------------|----------|--------------
ted | 1.2.3.4 | 10
jerry | 5.6.6.7 | 12
ted | 8.8.8.8 | 30
我希望查询的输出是:
jerry | 5.6.6.7 | 12
ted | 8.8.8.8 | 30
我可以在单个 sql 查询中执行此操作吗?如果重要,DBMS 是 Postgresql。
【问题讨论】:
标签: sql postgresql greatest-n-per-group