我正在使用来自汽车跟踪的数据的PostgreSQL数据库,该数据看起来与此类似。
+--------+---------------------+-------+---------+------------+------------+
| car_id | datetime | trip | speed | hub1_dist | hub2_dist |
+--------+---------------------+-------+---------+------------+------------+
| 1 | 2014-12-20 12:12:12 | 1 | 82 | 6.7 | 2.1 |
| 1 | 2014-12-20 12:12:13 | 1 | 81 | 3.5 | 4.4 |
| 1 | 2014-12-20 12:12:14 | 1 | 85 | 7.8 | 3.8 |
| 1 | 2014-12-20 17:42:51 | 2 | 82 | 4.2 | 5.4 |
| 1 | 2014-12-20 17:42:52 | 2 | 76 | 2.5 | 2.1 |
| 2 | 2014-12-21 09:24:24 | 3 | 80 | 4.5 | 6.7 |
| 2 | 2014-12-21 09:24:25 | 3 | 84 | 5.3 | 3.5 |
| 2 | 2014-12-21 09:24:26 | 3 | 83 | 8.2 | 5.1 |
+--------+---------------------+-------+---------+------------+------------+
每辆车都有特定的car_id
,并且trip
会基于该car_id
进行更改,或者datetime
的更改大于5秒。
为每个数据点注册speed
,并计算到多个hubs
的距离
我想根据到每个speed
的最短距离,最后得到一个显示hub
的表格。它应该看起来像这样:
+-------+------------------+------------------+
| trip | hub1_speed | speed_hubdist_2 |
+-------+------------------+------------------+
| 1 | 81 | 82 |
| 2 | 76 | 76 |
| 3 | 80 | 84 |
+-------+------------------+------------------+
我当前正在使用:
SELECT trip, hub1_dist, speed
FROM (
SELECT trip, hub1_dist, speed,
MIN(hub1_dist) OVER (PARTITION BY trip) AS MIN_hub1_dist
FROM data_base
) t
WHERE hub1_dist = MIN_hub1_dist
那行得通。但是在某些情况下,我有50 hubs
,所以我必须做50次。
答案 0 :(得分:0)
我认为您只想进行汇总:
select trip, min(hub1_dist), min(hub2_dist), min(hub3_dist), . . .
from data_base
group by trip;
您需要为每一列键入表达式。电子表格可以帮助您生成查询。或者,您可以执行以下操作:
select string_agg(replace('min(hub@n_dist) as hub@n_dist', '@n', gs.n::text), ', ')
from generate_series(1, 50, 1) gs(n)
编辑:
您可以使用数组函数获得最小距离的速度:
select trip, min(hub1_dist),
(array_agg(speed order by hub1_dist))[1] as speed_at_min1,
min(hub2_dist),
(array_agg(speed order by hub2_dist))[1] as speed_at_min2,
min(hub3_dist),
(array_agg(speed order by hub3_dist))[1] as speed_at_min3,
. . .
from data_base
group by trip;