【问题标题】:sql server union on geography columns地理列上的 sql server union
【发布时间】:2012-10-10 01:15:32
【问题描述】:

我使用的是 sql server 2008 R2,我有两个表,Regions 和 Facilities。两者都有一个包含地理元素的列。

我想计算地理元素交集的并集,如下所示:

SELECT * from Regions join Facilities on [Regions].[geography].STIntersects([Facilities].[geography])

这当然行不通。区域是大多边形,设施是点,每个点只包含在一个多边形中。

我可以写一些(伪代码)

for each r in Regions:
    for each f in Facilities:
        if f.[geography].STIntersects(r.[geography]):
            print r, f

但是使用数据库的全部意义在于对集合而不是元素进行操作,确定吗?

那么,有没有更好的方法来做到这一点?

谢谢 梅兰妮

【问题讨论】:

  • 这不是打印 (r,f) 吗? SELECT r.geography, f.geography from Regions r join Facilities f on r.geography.STIntersects(f.geography)=1
  • 是的,确实如此 - 将其作为答案,我会接受 - 谢谢。神奇之处在于将 =1 放在末尾。

标签: sql sql-server-2008 tsql sqlgeography


【解决方案1】:

STIntersect() 与所有布尔 SQL Server 函数一样,返回一个位,因此它是 0 或 1。
这将成为您的 WHERE 条件。

r 和 f 的 foreach 隐含在 JOIN 语句中。

SELECT r.geography, f.geography
from Regions r
join Facilities f on r.geography.STIntersects(f.geography)=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多