Database 仅比较时间戳列postgresql中的小时数
我有一张这样日期的桌子:Database 仅比较时间戳列postgresql中的小时数,database,postgresql,Database,Postgresql,我有一张这样日期的桌子: OBJECT TIMESTAMP_START TIMESTAMP_END House 2020-02-20 09:33:24 2020-02-20 09:33:33 Dog 2020-02-20 18:00:03 2020-02-21 18:33:22 Cat 2020-02-11 19:00:00 2020-02-11 19:15:23 我需要提取所有对象,开始时间戳和结束时间戳,其时间戳开始时间
OBJECT TIMESTAMP_START TIMESTAMP_END
House 2020-02-20 09:33:24 2020-02-20 09:33:33
Dog 2020-02-20 18:00:03 2020-02-21 18:33:22
Cat 2020-02-11 19:00:00 2020-02-11 19:15:23
我需要提取所有对象,开始时间戳和结束时间戳,其时间戳开始时间介于(18:00小时和09:00之间)
在这种情况下,是狗和猫
我怎样才能在postgreSql中做到这一点?你认为这容易吗
谢谢 您可以将时间戳强制转换为
time
select *
from the_table
where timestamp_start::time > time '18:00'
and timestamp_start::time < time '09:00'
选择*
从桌子上
其中时间戳_start::time>time'18:00'
和时间戳_start::time
由于您排除了这两个边界,因此以下情况很少是正确的:
select *
from tbl
where timestamp_start::time NOT BETWEEN time '09:00' AND time '18:00';
您不能单独使用时间来执行此操作,因为在小时内,09:00始终小于18:00,并且09:00到18:00是要排除的时间。您可以通过截断到起始位置并添加适当的间隔来实现这一点
with the_table (object, timestamp_start,timestamp_end ) as
( values ('House', '2020-02-20 09:33:24'::timestamp, '2020-02-20 09:33:33'::timestamp)
, ('Dog', '2020-02-20 18:00:03'::timestamp, '2020-02-21 18:33:22'::timestamp)
, ('Cat', '2020-02-11 19:00:00'::timestamp, '2020-02-11 19:15:23'::timestamp)
, ('Mouse', '2020-02-11 20:00:00'::timestamp, '2020-02-12 08:00:00'::timestamp)
)
select *
from the_table
where timestamp_start between
date_trunc('day', timestamp_start) + interval '18 hours' and
date_trunc('day', timestamp_start) + interval '1 day 9 hours' ;
当然,这会使所有这些行与时间匹配,即使它们已经存在了年。你可能也想考虑一下。只是一个建议 你是说第二天18:00到09:00之间?或者只是在09:00到18:00之间?嗯,我解释得很糟糕,对不起我的英语。开始时间>18:00,开始时间<09.00。在1800到0900之间执行的所有对象只需将时间戳强制转换为时间
(时间戳'2020-02-20 09:33:24')::time