如何构造多重选择查询错误:关系“ rxg”不存在

时间:2018-09-08 11:50:00

标签: sql postgresql postgis

我正在尝试对我的表执行查询: 所以这是详细的方法:

通过先恢复搜索来限制搜索:选择200公里以内的点的所有ID,    -然后选择两个点之间的距离为300并且Received_c相同的id -返回这些ID,按_id

分组
SELECT rxg._id 
FROM (
      SELECT *  FROM ais 
      WHERE ST_DWithin(geography(point), 
      ST_SetSRID(ST_MakePoint(-95.0217,29.6111), 4326)::geography, 
      200000)
) 
AS rxg, rxg AS b
WHERE ST_DWITHIN (rxg.point, b.point, 300) 
AND rxg._id != b._id
AND rxg.received_c = b.received_c
GROUP BY _id
;

1 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是,您尝试两次使用一个子查询,但是您不能这样做。相反,请尝试在此处使用公用表表达式(CTE),该表将对该子查询的代码内联两次:

WITH cte AS (
    SELECT *
    FROM ais 
    WHERE ST_DWithin(geography(point), 
        ST_SetSRID(ST_MakePoint(-95.0217,29.6111), 4326)::geography, 
        200000)
)

SELECT rxg._id
FROM cte rxg
INNER JOIN cte b
    ON rxg._id != b._id AND
       rxg.received_c = b.received_c
WHERE ST_DWITHIN (rxg.point, b.point, 300)
GROUP BY rxg._id;

还要注意,我用隐式内部联接替换了您的旧式隐式联接。自ANSI-92标准以来,这是连接表的首选方式。