【问题标题】:How to select in the query a time range of 1 hour?如何在查询中选择1小时的时间范围?
【发布时间】:2019-10-28 04:14:56
【问题描述】:

如何在查询中选择1小时的时间范围?

SQL

select * from 
  history 
where
  clock == current_timestamp - interval '01:00:00'::timestamp;

带有函数的数据 to_timestamp

28689   2019-10-26 21:02:09 83.6353
28689   2019-10-26 21:01:39 83.6614
28689   2019-10-26 21:01:09 82.9015
28689   2019-10-26 21:00:39 82.9039

输出

输入语法无效

所需的输出是一个选择,其中作为时间戳的clock 列在输入值的一小时范围内。

【问题讨论】:

标签: database postgresql


【解决方案1】:

您可以尝试从clock 列和当前时间戳中提取小时部分,然后比较两者之间的差异。

select *
from history 
where
    abs(extract(hour from clock) - extract(hour from current_timestamp)) <= 1;

如果您想要过去一小时内的所有记录,请尝试:

select *
from history
where extract(epoch from current_timestamp - clock) / 3600 <= 1;

【讨论】:

  • 您的第一个解决方案解决了我的问题,我只需要将 CLOCK 列向下排序,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2018-03-08
  • 1970-01-01
  • 2014-09-15
相关资源
最近更新 更多