简体   繁体   English

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

[英]Calculate distance between points using Long and Lat in SQL SERVER

I am using the below destination 我正在使用以下目的地

[BMAnalytics].[dbo].[EUACTIVESTORES]

And it has columns 它有专栏

[Store No]
[Lat]
[Long]

Can I use these columns to self Join the [Store No] so I have each combination of Store to Store listed in two columns, called 'Source' & 'Target' 我可以使用这些列自行加入[Store No],因此我将Store to Store的每个组合列在两列中,称为“Source”和“Target”

And then in a third column is it possible to calculate the distance between the two? 然后在第三列中可以计算两者之间的距离吗?

I have used the below previously but this only works for one single point to point, 我之前使用过以下内容,但这仅适用于单点到点,

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

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

Ideally, i'd like the distance from each branch to each branch etc. 理想情况下,我想要从每个分支到每个分支等的距离。

Any guidance is welcome 欢迎任何指导

Here is a quick example using a Self Join 以下是使用自我加入的快速示例

If you can, I would suggest adding a field to your source table which contains the Geography Point, and thus eliminate the need to calculate this value each time you run a query. 如果可以,我建议在源表中添加一个包含Geography Point的字段,从而无需在每次运行查询时计算此值。

Example

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]

Returns 返回

在此输入图像描述

EDIT If you can add a Geography Field 编辑如果您可以添加地理字段

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

And then the calculation would be 然后计算就是

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

FINAL QUERY LOOKS LIKE: 最终查询类似:

SELECT
      A.[STORE NO] AS 'SOURCE'
      ,B.[STORE NO] AS 'TARGET'
      ,CONVERT(DECIMAL(6,2),(((GEOGRAPHY::Point(A.[Lat], A.[Long], 4326).STDistance(GEOGRAPHY::Point(B.[Lat], B.[Long], 4326)))/1000)/8)*5) AS 'MILES'

FROM
      [bhxsql2014-dev].[BMAnalytics].[dbo].[EUACTIVESTORES] A
JOIN
      [bhxsql2014-dev].[BMAnalytics].[dbo].[EUACTIVESTORES] B on A.[Store No]<>B.[Store No]

WHERE 
         A.LAT        IS NOT NULL
     AND A.[STORE NO] IS NOT NULL
     AND B.LAT        IS NOT NULL
     AND B.[STORE NO] IS NOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找到几个点之间的距离-纬度/经度 - Finding the Distance Between Several Points- Lat/Long 在ms sql select命令中计算2点经纬度的距离 - calculate distance from 2 points lat and lon in ms sql select command 使用sql oracle计算地理点之间的距离 - Calculate distance between geo points using sql oracle 计算第一行经纬度与第二行经纬度之间的距离 - Calculate the distance between 1st row lat long with 2nd row lat long 使用SQL在一个纬度/经度坐标和一个固定坐标之间的近似距离 - Approximate distance between one lat/long coordinate and a fixed one using SQL 测量两个纬度/经度点之间的距离 - Measuring distance between two Lat/Lng points 如何在没有三角函数的情况下使用标准 SQL 计算两个纬度、经度点之间的距离(以英里为单位)? - How do I calculate distance between two latitude, longitude points in miles using standard SQL without trigonometry? Postgis计算点之间的距离 - Postgis calculate the distance between points 如何使用SQL查询来计算一组地理点的距离 - how to calculate distance of a set of geo points using sql query 在SQL中有效地找到2个纬度/长度之间的距离 - Efficiently finding the distance between 2 lat/longs in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM