Sql 如何计算一组结果中两行点之间的距离?

Sql 如何计算一组结果中两行点之间的距离?,sql,tsql,sql-server-2008-r2,common-table-expression,spatial,Sql,Tsql,Sql Server 2008 R2,Common Table Expression,Spatial,我有一个查询,它接受一个LINESTRING并将其转换为点的结果集 我搞不懂的是如何在这个结果集中找到两个特定行点之间的距离 这就是我到目前为止所做的: DECLARE @GeographyToConvert geography SET @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098

我有一个查询,它接受一个LINESTRING并将其转换为点的结果集

我搞不懂的是如何在这个结果集中找到两个特定行点之间的距离

这就是我到目前为止所做的:

DECLARE @GeographyToConvert geography
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints
例如,如何比较N=10和N=11之间的距离

这是我一直在尝试的,但它不起作用:

Declare @Point1 geography;
Declare @Point2 geography;

DECLARE @GeographyToConvert geography
--SET     @GeometryToConvert = (select top 1 geotrack from dbo.SYNCTESTING2 where geotrack is not null);
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)


SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints

select @Point1 = Point FROM GeometryPoints where N = 10;

select @Point2 = Point FROM GeometryPoints where N = 11

select @Point1.STDistance(@Point2) as [Distance in Meters]

这就是你要找的吗?到上一点的距离

DECLARE @GeographyToConvert geography
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point, PreviousPoint, DistanceFromPrevious) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1), CAST(NULL AS GEOGRAPHY), CAST(0 AS Float)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
            , @GeographyToConvert.STPointN(N)
            , @GeographyToConvert.STPointN(N).STDistance(@GeographyToConvert.STPointN(N + 1))
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText(), PreviousPoint, DistanceFromPrevious FROM GeographyPoints

这就是你要找的吗?到上一点的距离

DECLARE @GeographyToConvert geography
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point, PreviousPoint, DistanceFromPrevious) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1), CAST(NULL AS GEOGRAPHY), CAST(0 AS Float)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
            , @GeographyToConvert.STPointN(N)
            , @GeographyToConvert.STPointN(N).STDistance(@GeographyToConvert.STPointN(N + 1))
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText(), PreviousPoint, DistanceFromPrevious FROM GeographyPoints
替换

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints

只需根据需要更改@N1和@N2的值

替换

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints


只需根据需要更改@N1和@N2的值

分析函数就可以帮助您解决问题。它允许你获得下一个或上一个值。分析函数可以帮助你。它允许您获取下一个或上一个值。我希望能够手动选择要计算距离的两点。使用N列确定我想要哪一个2。这几乎就是我想能够手动选择计算距离的两点。使用N列确定我想要哪一个2。这几乎是它认为这正是我想做的!!!我用类似这样的东西编辑了我的第一篇文章,但就是无法正确理解语法。你就是那个男人!!!CTE有一个技巧,您只能访问该表一次,因此在这种情况下,您必须将值存储到临时表中,然后将其从临时表中取出。这正是我想做的!!!我用类似这样的东西编辑了我的第一篇文章,但就是无法正确理解语法。你就是那个男人!!!CTE有一个技巧,您只能访问该表一次,因此在这种情况下,您必须将值存储到临时表中,然后将其从临时表中取出。