[英]sql server union on geography columns
I'm using sql server 2008 R2, and I have two tables, Regions and Facilities. 我正在使用sql server 2008 R2,并且有两个表Regions和Facilities。 Both have a column containing a geography element. 两者都有一个包含地理元素的列。
I want to calculate the union on the intersection of the geograpy elements, like this: 我想在geograpy元素的交点上计算并集,如下所示:
SELECT * from Regions join Facilities on [Regions].[geography].STIntersects([Facilities].[geography])
which of course doesn't work. 哪个当然行不通。 The regions are large polygons, and the facilities are points each of which is contained in only one polygon. 区域是大多边形,设施是点,每个点仅包含在一个多边形中。
I can write some kind of (pseudocode) 我可以写某种(伪代码)
for each r in Regions:
for each f in Facilities:
if f.[geography].STIntersects(r.[geography]):
print r, f
but the whole point of using a database is to operate on the set not the elements, surely? 但是使用数据库的全部目的是要对集合而不是元素进行操作吗?
So, is there a better way to do this? 那么,有没有更好的方法可以做到这一点?
thanks Melanie 谢谢梅兰妮
STIntersect(), like all boolean SQL Server functions, returns a bit so it's 0 or 1. 像所有布尔SQL Server函数一样,STIntersect()返回的位为0或1。
That becomes your WHERE condition. 这成为您的WHERE条件。
The foreach of r and f are implicit in the JOIN statement. r和f的foreach隐含在JOIN语句中。
SELECT r.geography, f.geography
from Regions r
join Facilities f on r.geography.STIntersects(f.geography)=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.