-1

我是地理 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 创建点,或使用来自两个不同表的空间列。任何帮助,将不胜感激。

4

1 回答 1

0

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

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;

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

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

于 2018-12-02T21:35:32.803 回答