我有这样的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)
但在查询中我将它们划分为ulatitude和ulongtitude。所以我手动划分他们,但我不能再这样做,因为我有很多数据。
我可以在查询中添加什么内容,因此我采用此格式(55.56490549999999, 9.756640400000038)
的坐标并将它们分开。
答案 0 :(得分:2)
更新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]