我有一个如下的表:
X | Y | Z | node
----------------
1 | 2 | 3 | 100
2 | 2 | 3 |
2 | 2 | 4 |
2 | 2 | 5 | 200
3 | 2 | 5 |
4 | 2 | 5 |
5 | 2 | 5 | 300
X、Y、Z是一些点的三维空间坐标,一条曲线从第一行到最后一行经过所有对应的点,我需要计算“节点”列不为空的两个相邻点之间的曲线长度。
如果我能直接将结果插入到另一个有三列的表中,那就太好了:“第一节点”、“第二节点”、“曲线长度”。
我不需要在曲线中插入额外的点,只需要累加所有直线的长度,例如,为了计算节点100和200之间的曲线长度,我需要对3条直线的长度求和:(1、2、3)<->(2、2、3)(2、<->2、4)(2、2、4)<->(2、2、5)
EDIT该表有一个ID列,该列从第一行到最后一行按升序排列。
1条答案
按热度按时间8i9zcol21#
要在SQL中获取以前的值,请使用
lag
窗口函数,例如这样你就可以得到给定线段在三维空间中的上一个和下一个点,然后你就可以用regular geometric maths简单地计算线段的长度。
现在,您将获得每个段的长度(sqlfiddle查询),您可以使用
SELECT ... FROM (SELECT ...)
子查询或CTE(WITH ....
)项将其作为其他查询的输入。从节点段长度到节点到节点的长度是非常困难的,需要使用递归CTE或a window function创建一个包含空条目的表。
我最后得到了这个怪物:
分区技巧归功于How do I efficiently select the previous non-null value?。
结果:
若要直接插入另一个数据表,请使用
INSERT INTO ... SELECT ...
。