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

dar*_*ool 5 postgresql sql-timestamp

我有一个postgresql表,其中包含一个类型的列timestamp without timezone.每行增加/减少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
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个查询,它将选择某个日期范围之间的所有行,但也会在这些日期的特定时间范围内选择,例如:选择08年期间2015-05-20到2015-06-20之间的所有行: 00:00至16:00:00

到目前为止我尝试的所有内容似乎都不考虑日期+时间要求.

a_h*_*ame 10

我不确定我是否理解正确,但是如果您想要指定日期的所有行,但是从时间部分超出以下内容的行中排除行,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::' and '16:00:00'
Run Code Online (Sandbox Code Playgroud)

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

between运营商将包括边界(与时间恰在如行16:00:00).如果你不想,你需要将改变between条件相应><条件.

  • 在current_date + time '16:00之间和(current_date + 1):: timestamp之间` (2认同)