所以我有一个名为“船只”的船舶坐标的空间数据库。 它有以下表格:
SHIPS
============
id (serial)
vessel(uuid)
time(timestamp without timezone)
coord(geometry(Point, 4326)
我需要编写一个查询,返回其坐标为X米远的船只,时间间隔为Y。到目前为止我有这个:
SELECT DISTINCT ON (a.vessel, b.vessel) a.vessel, b.vessel
FROM ships a
INNER JOIN ships b
ON a.vessel = b.vessel
WHERE
ST_DWithin((ST_Transform(a.coord, 3035)),(ST_Transform(b.coord, 3035)), 5000)
AND (EXTRACT (EPOCH FROM(a.time - b.time))) <= 1500
AND (a.vessel != b.vessel);
但是如果我离开a.vessel!= b.vessel约束它总是返回null,如果我删除它,结果在两列上都是相同的容器。
答案 0 :(得分:0)
您在联接中使用了错误的条件:
SELECT DISTINCT ON (a.vessel, b.vessel) a.vessel, b.vessel
FROM ships a
INNER JOIN ships b
ON ST_DWithin((ST_Transform(a.coord, 3035)),(ST_Transform(b.coord, 3035)), 5000)
WHERE
(EXTRACT (EPOCH FROM(a.time - b.time))) <= 1500
AND (a.vessel != b.vessel);