我已经建立了一个非常简单的表格,代表 2D 环境中的点。Id 列是每个点的 id,geom 列是该点进入空间的二进制表示:
桌子public.foo
Column | Type | Modifiers
--------+----------------------+--------------------------------------------
id | integer | not null default nextval('mseq'::regclass)
geom | geometry(Point,2100) |
索引:
"foo_pkey" PRIMARY KEY, btree (id)
"foo_index_gist_geom" gist (geom)
为了找到从每个点到下一个点的距离,我使用了这个窗口函数:
select
id,
st_distance(geom,lag(geom,1) over (order by id asc)) distance
from
foo;
结果如下( st_distance(geom,geom) 给出了两个 geom 数据类型之间的距离):
id | distance
----+------------------
1 |
2 | 27746.1563439608
3 | 57361.8216245281
4 | 34563.3607734946
5 | 23421.2022073633
6 | 41367.8247514439
....
distance(1) -> null since its the first point
distance(2) -> ~28km from point 1 to point 2
distance(3) -> ~57km from point 2 to point 3
and etc..
我的目标是找到每个节点从开始到下一个点的累积距离。例如像下面这个模拟表:
id | distance | acc
----+------------------+-----
1 | |
2 | 27746.1563439608 | 27746.1563439608
3 | 57361.8216245281 | 85107.97797
4 | 34563.3607734946 | 119671.33874
where acc(1) is null because it is the first node,
acc(2) = acc(1) + dist(2)
acc(3) = acc(2) + dist(3)
and etc..
我尝试结合 sum 和 lag 函数,但 postgresql 说 windows 函数不能嵌套。我对如何继续感到完全困惑。谁能帮助我?