use*_*025 5 sql postgresql postgis window-functions
我已经建立了一个非常简单的表,用于表示2D环境中的点。Id列是每个点的id,geom列是空间中该点的二进制表示形式:
表 public.foo
Column | Type | Modifiers
--------+----------------------+--------------------------------------------
id | integer | not null default nextval('mseq'::regclass)
geom | geometry(Point,2100) |
Run Code Online (Sandbox Code Playgroud)
索引:
"foo_pkey" PRIMARY KEY, btree (id)
"foo_index_gist_geom" gist (geom)
Run Code Online (Sandbox Code Playgroud)
为了找到每个点到下一个点的距离,我正在使用此窗口函数:
select
id,
st_distance(geom,lag(geom,1) over (order by id asc)) distance
from
foo;
Run Code Online (Sandbox Code Playgroud)
结果如下(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..
Run Code Online (Sandbox Code Playgroud)
我的目标是找到每个节点从起点到下一点的累计距离。例如像下面的模拟表:
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..
Run Code Online (Sandbox Code Playgroud)
我尝试将sum和lag函数组合在一起,但是postgresql说Windows函数不能嵌套。我完全困惑如何进行。有人可以帮助我吗?
由于窗口函数不能覆盖另一个窗口函数(“不能嵌套”),因此需要添加子查询层(或 CTE):
SELECT id, sum(distance) OVER (ORDER BY id) AS cum_dist
FROM (
SELECT id, st_distance(geom, lag(geom, 1) OVER (ORDER BY id)) AS distance
FROM foo
) sub
ORDER BY id;
Run Code Online (Sandbox Code Playgroud)
这假设它id
是唯一的 - 这是由您的主键保证的。
归档时间: |
|
查看次数: |
2697 次 |
最近记录: |