使用时间戳列从PostgreSQL中选择最近10分钟的记录。

问题描述 投票:0回答:1

我在PostgreSQL中收集netflow数据包,我想只选择最近的记录,而不是所有的记录。

例如在这个命令中,我如何选择最近10分钟的记录?

SELECT ip_src, sum(bytes) 
FROM acct_v9 
where ip_src < '192.169.0.0' 
  and ip_src > '192.168.0.0' 
group by ip_src 
order by sum(bytes) 
DESC LIMIT 10 ;

如何选择最近10分钟的记录?

postgresql
1个回答
0
投票

假设你有一个 timestamp 列,存储行的创建时间(例如. created_at),你可以用这个。

SELECT ip_src, sum(bytes) 
FROM acct_v9 
where created_at >= current_timesteamp - interval '10 minute' 
  and ip_src < '192.169.0.0' 
  and ip_src > '192.168.0.0' 
group by ip_src 
order by sum(bytes) 
DESC LIMIT 10 ;
© www.soinside.com 2019 - 2024. All rights reserved.