postgres - 在T1中每行的时间戳之前选择T1中的最新行

时间:2017-01-25 17:15:45

标签: postgresql gps postgis postgresql-9.3

我回答了多个问题,但没有找到答案。所以我希望得到类似解决方案的答案/链接。 我有两张桌子:车辆和活动。每辆车都有多个GPS位置 - 比如玩具示例中有4辆车有11排GPS ping。但是对玩具测试说只有2个事件。

例如车辆(每行都有唯一的gid,每辆车都有vehicle_id,还有时间戳date_time和位置geom):

SELECT gid, vehicle_id, date_time, geom FROM test_v vehicles ORDER BY 2,3,1;


   gid   | vehicle_id |      date_time      |                        geom                        
---------+------------+---------------------+----------------------------------------------------
 1257854 |        742 | 2009-06-06 19:02:01 | 01010000208A7D0000667CC002A7262741A444A87995324041
 1258089 |        742 | 2009-06-06 19:03:29 | 01010000208A7D00007ABCA45AD22227415A5ADEB5662E4041
 1258842 |        742 | 2009-06-06 19:07:23 | 01010000208A7D000083D5151C7414274196D0D21AE7284041
 1259213 |        742 | 2009-06-06 19:09:11 | 01010000208A7D000054160E1FE4052741D6E2E74CD2284041
 1257939 |       3055 | 2009-06-06 19:02:31 | 01010000208A7D0000327D6277FAF426418EA975ECB6504041
 1258898 |       3055 | 2009-06-06 19:07:41 | 01010000208A7D000003519FB5D5F6264166B1B943C3514041
 1259313 |       3425 | 2009-06-06 19:09:35 | 01010000208A7D0000418D23C296442741FCFCEF0069394041
 1258008 |       3426 | 2009-06-06 19:02:56 | 01010000208A7D000011958634824027414D5D3823A43C4041
 1258307 |       3426 | 2009-06-06 19:04:37 | 01010000208A7D0000C8017753AB38274175E727CBA43A4041
 1258744 |       3426 | 2009-06-06 19:06:56 | 01010000208A7D00005F9811D7903A274145135F0589384041
 1259018 |       3426 | 2009-06-06 19:08:13 | 01010000208A7D0000546B478B323A2741F4CB8DB6AD374041
(11 rows)

事件(唯一gid,时间戳t_date和位置geom):

SELECT gid, t_date, geom FROM test_c events;
  gid  |       t_date        |                        geom                        
-------+---------------------+----------------------------------------------------
 13009 | 2009-06-06 19:04:31 | 01010000208A7D000034F06A423F18274123DF6756B8194041
 13021 | 2009-06-06 19:08:53 | 01010000208A7D0000D093E2A470FE26413E935C3A5E304041

我们的想法是了解每个事件发生时的情况,哪些车辆更近,后来优化车辆分配到事件。

所以我想加入这些表格,这样我就可以获得最多4行(车辆数量)的每个事件,其中每个车辆的最后已知位置在事件发生前120分钟的时间段内直到事件发生时间,按两者之间的距离排序。我希望它们在同一个表中,以便我可以按时间,位置,事件类型等进行比较。

现在这里是我被困的地方。我知道如何在特定的时间段内找到最新的车辆记录(比如19:10)。但这不是我需要的,因为它在19:10时切断了车辆的表格。但对于gid= 13009时间为19:04:31的事件,可能会包含一个比事件晚的位置。我想要的是最新的位置,直到事件发生的时间显然与每个事件不同。

我试过了:

SELECT DISTINCT ON (1)
  v.vehicle_id
  , row_number() OVER() as gid
  , st_distance(v.geom, c.geom)::float4 as distance_m
  , c.gid as c_gid 
  , v.gid as v_gid
  , c.t_date as dt_c 
  , v.date_time as dt_v 
  , (c.t_date - v.date_time) as d_t
FROM (SELECT * FROM test_c ) as c
  ,  (SELECT * FROM test_v ORDER BY date_time DESC )  v
WHERE date_time >= (c.t_date -  '120 minute' :: INTERVAL) AND date_time <= (c.t_date + '0 minute' :: INTERVAL)
ORDER BY 1, 7  DESC , 5, 6 ,  c.geom <-> v.geom ASC ;

这就是我得到的:

 vehicle_id | gid | distance_m | c_gid |  v_gid  |        dt_c         |        dt_v         |   d_t    
------------+-----+------------+-------+---------+---------------------+---------------------+----------
        742 |   3 |     4748.6 | 13021 | 1258842 | 2009-06-06 19:08:53 | 2009-06-06 19:07:23 | 00:01:30
       3055 |   2 |    17125.8 | 13021 | 1258898 | 2009-06-06 19:08:53 | 2009-06-06 19:07:41 | 00:01:12
       3426 |   1 |    8515.65 | 13021 | 1259018 | 2009-06-06 19:08:53 | 2009-06-06 19:08:13 | 00:00:40
(3 rows)

我得到的日期不正确,每辆车只有一次(我猜是因为DISTINCT)但是每个事件都需要一次,并且有适当的最后位置(所以在这个例子中2次为2次事件)。

理想情况下,我还希望下一步为每辆车提供3个最新位置,但也许以后也是如此。

1 个答案:

答案 0 :(得分:1)

这就是我的理解:

select *
from (
    select distinct on (c.gid, v.vehicle_id)
        v.vehicle_id
        , row_number() over(order by v.gid) as gid
        , st_distance(v.geom, c.geom)::float4 as distance_m
        , c.gid as c_gid 
        , v.gid as v_gid
        , c.t_date as dt_c 
        , v.date_time as dt_v 
        , (c.t_date - v.date_time) as d_t
    from
        test_c c
        inner join 
        test_v v on
            date_time between c.t_date - '120 minute' :: interval and c.t_date
    order by c.gid, v.vehicle_id, v.date_time desc
) s
order by c_gid, distance_m
;
 vehicle_id | gid | distance_m | c_gid |  v_gid  |        dt_c         |        dt_v         |   d_t    
------------+-----+------------+-------+---------+---------------------+---------------------+----------
        742 |   8 |    10674.9 | 13009 | 1258089 | 2009-06-06 19:04:31 | 2009-06-06 19:03:29 | 00:01:02
       3426 |   5 |    18607.5 | 13009 | 1258008 | 2009-06-06 19:04:31 | 2009-06-06 19:02:56 | 00:01:35
       3055 |   4 |    28516.8 | 13009 | 1257939 | 2009-06-06 19:04:31 | 2009-06-06 19:02:31 | 00:02:00
        742 |  11 |     4748.6 | 13021 | 1258842 | 2009-06-06 19:08:53 | 2009-06-06 19:07:23 | 00:01:30
       3426 |  13 |    8515.65 | 13021 | 1259018 | 2009-06-06 19:08:53 | 2009-06-06 19:08:13 | 00:00:40
       3055 |  12 |    17125.8 | 13021 | 1258898 | 2009-06-06 19:08:53 | 2009-06-06 19:07:41 | 00:01:12