SQL Spatial Join

雨燕双飞 提交于 2019-12-03 17:47:41

问题


I have 2 tables one with points as geographies and other with polygons as geographies. I am able to find which polygon a single point falls(from the point table) by the following query:

DECLARE @p geography;
select @p = PointGeom from dbo.PointTable where ID = 1 
SELECT  a.ID, ATTRIBUTE1, geom 
from dbo.PolygonTable  a
where geom.STIntersects(@p) = 1;

However, I want to do a join between the two tables and get the polygons in which each of the points in the Point Table fall. Is it even possible? Or do I need to loop through the Point table and call the above query multiple times?


回答1:


This should work:

SELECT 
    polyTable.[PolygonID]
,   pointTable.[PointID]
FROM 
[PolygonTable_Name] polyTable WITH(INDEX([SPATIAL_INDEX_NAME]))
INNER JOIN 
[PointTabl_Name] pointTable
ON
polyTable.Geog.STIntersects(pointTable.Geog) = 1

I have added an index hint " WITH(INDEX(...)) " as this will speed up the query.



来源:https://stackoverflow.com/questions/7655408/sql-spatial-join

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!