Postgres - 查询时间序列模式

时间:2018-03-27 20:44:53

标签: postgresql datetime

我在Postgres有一个点模式,其中每个出租车旅行的接送时间和位置记录了几个月。我需要计算给定间隔(例如3月5日至3月27日之间)的时间窗口(例如,在00:00和03:00之间)的出租车行程的数量。因此,对于给定的示例,我需要计算在5点到6点,7点,3点和3月27日在00:00和03:00之间发生的总出租车行程。

我找到的唯一一个函数是'date_trunc',但我并不认为它是合适的,因为窗口大小已经固定。

1 个答案:

答案 0 :(得分:1)

drop table if exists taxi_trips;

create table taxi_trips
( pickup timestamp
, dropoff timestamp null
, notes varchar(100)
);

insert into taxi_trips (pickup,  dropoff, notes) values ('2/28/2018 07:15', '2/28/2018 7:35', 'not found, date too early');
insert into taxi_trips (pickup,  dropoff, notes) values ('3/5/2018 01:15', '3/5/2018 1:35', 'found');
insert into taxi_trips (pickup,  dropoff, notes) values ('3/5/2018 06:15', '3/5/2018 6:35', 'not found, outside time window');
insert into taxi_trips (pickup,  dropoff, notes) values ('3/6/2018 01:15', '3/6/2018 1:35', 'found');
insert into taxi_trips (pickup,  dropoff, notes) values ('3/6/2018 06:15', '3/6/2018 6:35', 'not found, outside time window');
insert into taxi_trips (pickup,  dropoff, notes) values ('4/1/2018 07:15', '4/1/2018 7:35', 'not found, date too late');

select count(*)
from taxi_trips
where pickup between '3/5/2018' and '3/28/2018'
and extract (hour from pickup) between 0 and 3
相关问题