【问题标题】:Geography Select Query地理选择查询
【发布时间】:2013-10-22 07:10:30
【问题描述】:

我有一个 SQL Server 2012 位置表,其中有一列 geo 类型为 geography

我有一个链接到位置表的用户表

位置

locationId int
geo geography

用户

userId int
locationId int

我会知道该位置的 ID,并且我知道如何使用 stdistance 进行距离查询。但是,在查询中比较两个距离的最佳方法是什么。我可以使用子选择来做到这一点,但有没有更好的方法

子选择看起来像

select 
   suburb, state, postcode, 
   geo.STDistance((select geo from location where locationId = 47))/1000 kms
from location
order by kms desc

【问题讨论】:

    标签: sql sql-server geography sqlgeography


    【解决方案1】:

    47 号位置有什么特别之处吗?会一直是 47 吗?无论哪种方式,您都可以将其粘贴在变量中

    declare @HQ geography;
    select @HQ = geo from location where locationid = 47;
    
    select 
       suburb, state, postcode, 
       geo.STDistance(@HQ)/1000 kms
    from location
    order by kms desc
    

    如果您(无论出于何种原因)想要在一个查询中全部完成,您可以尝试外部应用

    select 
       suburb, state, postcode, 
       geo.STDistance(HQ.geo)/1000 kms
    from location
    outer apply (select geo from location where locationid = 47) as HQ
    order by kms desc
    

    【讨论】:

    • select @HQ geo from location where locationid = 47; 应该是 select @HQ = geo from location where locationid = 47; 注意:缺少 = 符号
    • 没错。固定!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2017-03-30
    • 2021-09-27
    • 2015-05-22
    相关资源
    最近更新 更多