我已经在SQL Server中计算了地理距离过程,但是它具有别名列,并且无法将排序属性设置为true;
DECLARE
@GEO1 GEOGRAPHY,
@LAT VARCHAR(10),
@LONG VARCHAR(10)
SET @LAT = '41.000000'
SET @LONG = '30.000000'
SET @geo1 = geography::Point(@LAT, @LONG, 4326)
SELECT
LOC_ID, LOC_NAME,
LEFT(CONVERT(VARCHAR, (@geo1.STDistance(geography::Point(ISNULL(LAT,0), ISNULL(LONG, 0), 4326))) / 1000), 4) AS DISTANCE
FROM
Konum
WHERE
(@geo1.STDistance(geography::Point(ISNULL(LAT,0), ISNULL(LONG,0), 4326))) / 1000 < 10
我尝试了
DECLARE
@GEO1 GEOGRAPHY,
@LAT VARCHAR(10),
@LONG VARCHAR(10)
SET @LAT = '41.329867'
SET @LONG = '36.264149'
SET @geo1= geography::Point(@LAT, @LONG, 4326)
SELECT
LOC_ID, LOC_NAME,
LEFT(CONVERT(VARCHAR, (@geo1.STDistance(geography::Point(ISNULL(LAT, 0), ISNULL(LONG, 0), 4326))) / 1000), 4) AS DISTANCE
FROM
Konum
WHERE
(@geo1.STDistance(geography::Point(ISNULL(LAT, 0), ISNULL(LONG, 0), 4326))) / 1000 < 30
ORDER BY
DISTANCE ASC -- <=
但是返回别名varchar列
1.2
1.3
11.5 <= Not like
2.8
9.7
我尝试过:
CAST(DISTANCE AS INT)
但是SMSS抛出错误“无效的列名'DISTANCE'”,因为它是列别名。
如何解决此问题?
答案 0 :(得分:0)
您的距离列当前是一个字符串,因此将按此排序。删除字符串转换(LEFT(CONVERT(VARCHAR
),它将正确排序。
DECLARE @GEO1 GEOGRAPHY, @LAT VARCHAR(10) = '41.329867', @LONG VARCHAR(10) = '36.264149'
SET @geo1= geography::Point(@LAT, @LONG, 4326)
SELECT LOC_ID, LOC_NAME
, (@geo1.STDistance(geography::Point(ISNULL(LAT,0),
ISNULL(LONG,0), 4326)))/1000 as DISTANCE
from Konum
WHERE (@geo1.STDistance(geography::Point(ISNULL(LAT,0), ISNULL(LONG,0), 4326)))/1000 < 30
order by DISTANCE ASC