如何在 SQL 查询中将我的坐标划分为经度和纬度
How to devide my Coordinates into longtitue and latitude in a SQL query
我有这样的 sql 个查询:
Select Userid, sum(cast(distanceinKM as int)) as KM, day from(
SELECT [User].Userid
,[User].ulatitude, [User].ulongitude, [Checkpoint].clatitude, [Checkpoint].clongitutde,
(geography::Point([User].ulatitude, [User].ulongitude, 4326)).STDistance
(geography::Point([Checkpoint].clatitude, [Checkpoint].clongitutde, 4326))/100 as distanceinKM, cast(Timestamp as date) as day
FROM [User] INNER JOIN
Tablelog ON [User].Userid = Tablelog.Userid INNER JOIN
[Checkpoint] ON Tablelog.checkpointid = [Checkpoint].checkpiontid ) as distance
where Day(day) = 6 AND MONTH (day) = 9 AND YEAR (day) = 2011
group by userid , day
函数运行良好,但坐标有问题。在我的数据库中,我的坐标以这种方式存储:(55.56490549999999, 9.756640400000038)
但在查询中我将它们分为 ulongtitude 和 ulongtitude。所以我手动划分它们,但我不能再这样做了,因为我有很多数据。
我可以在查询中添加什么,所以我采用这种格式的坐标 (55.56490549999999, 9.756640400000038)
并将它们相除。
已更新 v-4
我已更新查询以适合确切的数据格式。请尝试:
Select
distance.Userid,
sum(cast(distance.distanceinKM as int)) as KM,
distance.[day]
from
(
SELECT
UC_Lat_Long.Userid,
UC_Lat_Long.ulatitude,
UC_Lat_Long.ulongitude,
UC_Lat_Long.clatitude,
UC_Lat_Long.clongitutde,
(geography::Point(UC_Lat_Long.ulatitude, UC_Lat_Long.ulongitude, 4326)).STDistance(geography::Point(UC_Lat_Long.clatitude, UC_Lat_Long.clongitutde, 4326))/100 as distanceinKM,
UC_Lat_Long.[day]
FROM
(
SELECT
[User].Userid,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([User].Coordinates, '0,0'), 1, CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) - 1)))) AS ulatitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([User].Coordinates, '0,0'), CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) + 1, LEN(ISNULL([User].Coordinates, '0,0')) - CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) - 1)))) AS ulongitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([Checkpoint].coordinates, '0,0'), 1, CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) - 1)))) AS clatitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([Checkpoint].coordinates, '0,0'), CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) + 1, LEN(ISNULL([Checkpoint].coordinates, '0,0')) - CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) - 1)))) AS clongitutde,
cast([Timestamp] as date) as [day]
FROM
[User]
INNER JOIN Tablelog ON [User].Userid = Tablelog.Userid
INNER JOIN [Checkpoint] ON Tablelog.checkpointid = [Checkpoint].checkpiontid
) AS UC_Lat_Long
) as distance
where
Day(distance.[day]) = 6
AND MONTH (distance.[day]) = 9
AND YEAR (distance.[day]) = 2011
group by
distance.Userid,
distance.[day]
我有这样的 sql 个查询:
Select Userid, sum(cast(distanceinKM as int)) as KM, day from(
SELECT [User].Userid
,[User].ulatitude, [User].ulongitude, [Checkpoint].clatitude, [Checkpoint].clongitutde,
(geography::Point([User].ulatitude, [User].ulongitude, 4326)).STDistance
(geography::Point([Checkpoint].clatitude, [Checkpoint].clongitutde, 4326))/100 as distanceinKM, cast(Timestamp as date) as day
FROM [User] INNER JOIN
Tablelog ON [User].Userid = Tablelog.Userid INNER JOIN
[Checkpoint] ON Tablelog.checkpointid = [Checkpoint].checkpiontid ) as distance
where Day(day) = 6 AND MONTH (day) = 9 AND YEAR (day) = 2011
group by userid , day
函数运行良好,但坐标有问题。在我的数据库中,我的坐标以这种方式存储:(55.56490549999999, 9.756640400000038)
但在查询中我将它们分为 ulongtitude 和 ulongtitude。所以我手动划分它们,但我不能再这样做了,因为我有很多数据。
我可以在查询中添加什么,所以我采用这种格式的坐标 (55.56490549999999, 9.756640400000038)
并将它们相除。
已更新 v-4
我已更新查询以适合确切的数据格式。请尝试:
Select
distance.Userid,
sum(cast(distance.distanceinKM as int)) as KM,
distance.[day]
from
(
SELECT
UC_Lat_Long.Userid,
UC_Lat_Long.ulatitude,
UC_Lat_Long.ulongitude,
UC_Lat_Long.clatitude,
UC_Lat_Long.clongitutde,
(geography::Point(UC_Lat_Long.ulatitude, UC_Lat_Long.ulongitude, 4326)).STDistance(geography::Point(UC_Lat_Long.clatitude, UC_Lat_Long.clongitutde, 4326))/100 as distanceinKM,
UC_Lat_Long.[day]
FROM
(
SELECT
[User].Userid,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([User].Coordinates, '0,0'), 1, CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) - 1)))) AS ulatitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([User].Coordinates, '0,0'), CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) + 1, LEN(ISNULL([User].Coordinates, '0,0')) - CHARINDEX(',', ISNULL([User].Coordinates, '0,0')) - 1)))) AS ulongitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([Checkpoint].coordinates, '0,0'), 1, CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) - 1)))) AS clatitude,
CONVERT(NUMERIC(28,8), LTRIM(RTRIM(SUBSTRING(ISNULL([Checkpoint].coordinates, '0,0'), CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) + 1, LEN(ISNULL([Checkpoint].coordinates, '0,0')) - CHARINDEX(',', ISNULL([Checkpoint].coordinates, '0,0')) - 1)))) AS clongitutde,
cast([Timestamp] as date) as [day]
FROM
[User]
INNER JOIN Tablelog ON [User].Userid = Tablelog.Userid
INNER JOIN [Checkpoint] ON Tablelog.checkpointid = [Checkpoint].checkpiontid
) AS UC_Lat_Long
) as distance
where
Day(distance.[day]) = 6
AND MONTH (distance.[day]) = 9
AND YEAR (distance.[day]) = 2011
group by
distance.Userid,
distance.[day]