Select 每天特定时间范围的时间戳数据

Select timestamp data for specific time range each day

我有一个带有 timestamp without timezone 类型列的 postgresql table。每行 increases/decreases 1 分钟 例如:

2015-07-28 01:35:00
2015-07-28 01:34:00
2015-07-28 01:33:00
...
...
2015-07-27 23:59:00
2015-07-27 23:58:00
2015-07-27 23:57:00

我正在尝试编写一个查询,该查询将 select 特定日期范围内的所有行,但也在那些日子的特定时间范围内,例如:Select 2015 年之间的所有行-05-20 到 2015-06-20 08:00:00 到 16:00:00.

到目前为止我尝试过的一切似乎都没有同时考虑日期和时间要求。

我不确定我是否理解正确,但是如果您想要指定日期的所有行,但排除时间部分在 08:0016:00 之外的那些日期的行以下应该这样做:

select *
from the_table
where the_column::date between date '2015-05-20' and date '2015-06-20'
  and the_column::time between time '08:00:00' and '16:00:00'

表达式 the_column::date 称为强制转换,会将 timestamp 转换为 date 并删除时间信息。 the_column::time 提取 timestamp 列的时间部分。

between 运算符将包含边界(例如,时间正好在 16:00:00 的行)。如果您不想这样,您需要将 between 条件更改为相应的 >< 条件。