gpt4 book ai didi

sql - 在SQL SERVER中使用Long和Lat计算点之间的距离

转载 作者:行者123 更新时间:2023-12-03 02:26:53 25 4
gpt4 key购买 nike

我正在使用以下目的地

[BMAnalytics].[dbo].[EUACTIVESTORES]

它有列

[Store No]
[Lat]
[Long]

我可以使用这些列自行加入[商店编号],以便我将商店到商店的每个组合列在两列中,称为“源”和“目标”

然后在第三列中是否可以计算两者之间的距离?

我之前使用过以下内容,但这仅适用于单个点对点,

DECLARE @source geography = 'POINT(0 51.5)'
DECLARE @target geography = 'POINT(-3 56)'

SELECT (@source.STDistance(@target))/1000

理想情况下,我想要每个分支到每个分支的距离等。

欢迎任何指导

最佳答案

这是一个使用自连接的简单示例

如果可以的话,我建议在源表中添加一个包含地理点的字段,这样就无需在每次运行查询时计算该值。

示例

Declare @YourTable table ([Store No] int,Lat float,Lng float)
Insert Into @YourTable values
(1,-8.157908, -34.931675)
,(2,-8.164891, -34.919033)
,(3,-8.159999, -34.939999)

Select [From Store] = A.[Store No]
,[To Store] = B.[Store No]
,Meters = GEOGRAPHY::Point(A.[Lat], A.[Lng], 4326).STDistance(GEOGRAPHY::Point(B.[Lat], B.[Lng], 4326))
From @YourTable A
Join @YourTable B on A.[Store No]<>B.[Store No]

返回

enter image description here

EDIT If you can add a Geography Field

Update YourTable Set GeoPoint = GEOGRAPHY::Point([Lat], [Lng], 4326)

然后计算结果为

,Meters = A.GeoPoint.STDistance(B.GeoPoint)

关于sql - 在SQL SERVER中使用Long和Lat计算点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45483254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com