mariadb 在空间函数中使用窗函数是否有效?

nnvyjq4y  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(96)

我在计算追踪器移动的总距离。然而,当我试图将窗口函数与空间函数一起使用时,我发现了一些奇怪的错误。
如果我写一个这样的查询,它是有效的:

select
   t.createdAt,
   ST_AsText(lead(t.location) over (ORDER BY t.createdAt desc)),
   st_astext(t.location)
from track t
order by t.createdAt desc
limit 1,1;

同样,这是有效的:

select
   t.createdAt,
   st_astext(t.location),
   st_distance_sphere(t.location, t.location)
from track t
order by t.createdAt desc
limit 1,1;

但是当我试着这样使用它时,它给了我一个错误:

select
   t.createdAt,
   ST_AsText(lead(t.location) over (ORDER BY t.createdAt desc)),
   st_astext(t.location),
   st_distance_sphere(t.location, t.location)
from track t
order by t.createdAt desc
limit 1,1;
Out of range error: Longitude should be [-180,180] in function ST_Distance_Sphere

我可以将空间函数与窗口函数一起使用吗?这是MariaDB的限制吗?还是我的查询写错了
编辑:
我发现了一个非常旧的无效位置记录,我删除了它。现在我的查询都运行良好。但我不知道为什么旧的记录会导致错误。我认为上述查询应该只访问最新的记录。

ogsagwnx

ogsagwnx1#

看起来你正在使用**ST_Distance_Sphere(t.location,t.location)**来计算位置与自身之间的距离。这可能会导致“Out of range error:经度应该是[-180,180]”的误差,因为函数需要两个不同的坐标来计算距离,但相同的坐标被提供了两次。
请尝试下面的查询,让我知道它是否有效:

SELECT
   t.createdAt,
   ST_AsText(LEAD(t.location) OVER (ORDER BY t.createdAt DESC)),
   ST_AsText(t.location),
   LEAD(ST_AsText(t.location)) OVER (ORDER BY t.createdAt DESC),
   CASE
      WHEN ST_AsText(t.location) IS NOT NULL AND LEAD(ST_AsText(t.location)) OVER (ORDER BY t.createdAt DESC) IS NOT NULL THEN
         ST_Distance_Sphere(t.location, ST_GeomFromText(LEAD(ST_AsText(t.location)) OVER (ORDER BY t.createdAt DESC)))
      ELSE NULL
   END AS distance
FROM track t
ORDER BY t.createdAt DESC
LIMIT 1, 1;
vatpfxk5

vatpfxk52#

1.这个错误是由地球上不存在的一个值引起的。地球是一个球形椭球体,坐标必须在经度-180到+180和纬度-90到+90之间。
1.在另一个函数中使用窗口函数的结果没有限制,因此它也适用于空间函数。
范例:

SELECT createdAt, ST_AsText(location) AS current_location, 
       ST_AsText(LEAD(location) OVER (ORDER BY createdAt desc))
       as next_location,
       ST_Distance_Sphere(location, LEAD(location) OVER
       (ORDER BY createdAt desc))/1000 AS distance_km
FROM track;

相关问题