如何基于30分钟的时间间隔检索时间戳列的最小和最大时间?

时间:2019-05-09 06:59:34

标签: sql postgresql timestamp data-science data-analysis

我正在尝试提取所需的输出,如下所示

   Driver_ID|   Interval_Start_Time     |  Interval_End_Time      |    Clocked_In_Time      |   Clocked_Out_Time      |  Score
    232     |  2019-04-02 00:00:00.000  | 2019-04-02 00:30:00.000 | 2019-04-02 00:10:00.000 | 2019-04-02 00:29:00.000 |  0.55

我的目标是在30分钟或每半小时的时间间隔中提取ID,并以相同的30分钟或半小时的时间间隔将它们的最小或最早计时时间和最大或最新计时时间拉出。

我当前的查询是

WITH TIME AS
  (SELECT DISTINCT CASE
                       WHEN extract(MINUTE
                                    FROM offer_time_utc)<30 THEN date_trunc('hour', offer_time_utc)
                       ELSE date_add('minute',30, date_trunc('hour', offer_time_utc))
                   END AS interval_start_time,
                   CASE
                       WHEN extract(MINUTE
                                    FROM offer_time_utc)<30 THEN date_add('minute',30, date_trunc('hour', offer_time_utc))
                       ELSE date_add('hour',1, date_trunc('hour', offer_time_utc))
                   END AS interval_end_time
   FROM integrated_delivery.trip_offer_fact offer
   WHERE offer.business_day = date '2019-04-01' )
SELECT DISTINCT offer.Driver_ID,
                offer.region_uuid,
                interval_start_time,
                interval_end_time,
                min(sched.clocked_in_time_utc) AS clocked_in_time,
                max(sched.clocked_out_time_utc) AS clocked_out_time,
                cast(scores.acceptance_rate AS decimal(5,3)) AS acceptance_rate
FROM integrated_delivery.trip_offer_fact offer
JOIN TIME ON offer.offer_time_utc BETWEEN time.interval_start_time AND time.interval_end_time
JOIN integrated_delivery.courier_actual_hours_fact sched ON offer.Driver_ID = sched.Driver_ID
JOIN integrated_product.driver_score_v2 scores ON offer.Driver_ID = scores.courier_id
AND offer.region_uuid = scores.region_id
AND offer.region_uuid = sched.region_uuid
AND offer.business_day = date '2019-04-01'
AND sched.business_day = date '2019-04-01'
AND scores.extract_dt = 20190331
AND offer.region_uuid IN('930c534f-a6b6-4bc1-b26e-de5de8930cf9')
GROUP BY 1,2,3,4,7

但是,在以下正确的时间间隔内,似乎并没有给我正确的最小和最大计时时间,

    driver_uuid  region_uuid   interval_start_time      interval_end_time           clocked_in_time         clocked_out_time      score 
      232              bbv     2019-04-01 14:30:00.000   2019-04-01 15:00:00.000  2019-04-01 14:43:13.140  2019-04-01 22:30:46.043   0.173

当我在这两行中添加时,

JOIN TIME ON sched.clocked_in_time_utc BETWEEN time.interval_start_time AND time.interval_end_time

jOIN TIME ON sched.clocked_out_time_utc BETWEEN time.interval_start_time AND time.interval_end_time

iIt给我一个错误,因为我认为那是不正确的。 如何在正确的时间间隔内正确输入最小和最大时钟输入和时钟输出时间?意思是我只希望最早的计时时间和最近的计时时间在每半小时的开始和结束时间中。

我很感激任何人在寻找!谢谢

0 个答案:

没有答案