【问题标题】:SQL STDistance where both locations are in same record两个位置都在同一记录中的 SQL STDistance
【发布时间】:2018-12-01 22:17:25
【问题描述】:

我是地理 SQL 查询的新手。我有两张桌子; [station_information] 描述具有地理类型空间列的自行车租赁站,以及 [bike_rides] 包含有关租赁自行车旅行的信息。

[station_information] 有 station_id 作为主键,[bike_rides] 有 to_station_id 和 from_station_id 列,引用自行车旅行的起点和终点。我想在 [bike_rides] 中创建一个距离列,其中包含每条记录的 from_station_id 和 to_station_id 之间的距离。

我该怎么做呢?我知道我必须加入表格并使用 STDistance,但我不知道。我查找的每个 STDistance 示例都为起点和终点创建变量,并使用 STGeomFromText 创建点,或使用来自两个不同表的空间列。任何帮助将不胜感激。

【问题讨论】:

  • 样本数据(如 DDL 和 DML)和预期结果将真正帮助您在这里得到答案。

标签: sql sql-server geography sqlgeography ogc


【解决方案1】:

我正在设想您的架构是这样的:

create table dbo.station_information (
   station_id int not null
      constraint PK_station_information primary key clustered (station_id),
   location geography not null
);

create table  dbo.bike_rides (
    bike_ride_id int not NULL
        constraint PK_bike_rides primary key clustered (bike_ride_id),
    from_station_id INT
        constraint [FK_bike_rides_origin]
        foreign key (from_station_id)
        references dbo.station_information (station_id),
    to_station_id INT
        constraint [FK_bike_rides_destination]
        foreign key (to_station_id)
        references dbo.station_information (station_id)
);

如果是这样,从概念上讲,以下查询应该会让您克服困难:

select br.bike_ride_id, 
   origin.[location].STDistance(destination.[location])
from dbo.bike_rides as br
join dbo.station_information as origin
    on br.from_station_id = origin.station_id
join dbo.station_information as destination
    on br.to_station_id = destination.station_id;

查询中的连接只是正常的“连接到具有我想要的详细信息的地方”。唯一奇怪的事情(如果您可以称其为奇怪的话)是您有两列引用同一个表,因此您必须两次连接回该表(一次用于您想要获取详细信息的每个上下文)。

一旦执行了这些连接,您就可以使用源地理列和目标地理列,因此可以对它们进行任何您想要的地理空间计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多