SQL STDistance,其中两个位置位于同一记录中

SQL STDistance,其中两个位置位于同一记录中,sql,sql-server,geography,sqlgeography,ogc,Sql,Sql Server,Geography,Sqlgeography,Ogc,我不熟悉地理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之间

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

我设想您的模式如下:

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;
查询中的连接只是普通的“连接回具有我想要的详细信息的位置”。唯一奇怪的事情(如果你可以称之为怪异的话)是,你有两列引用同一个表,所以你必须连接回该表两次(对于你想要获取细节的每个上下文一次)

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

示例数据(如DDL和DML)和预期结果将真正帮助您获得答案。