GTFS在旅行中获得最后一站

时间:2015-09-01 09:23:23

标签: mysql sql gtfs

使用标准的 GTFS 数据库,我试图在当前查询的行程中添加最后一站的名称,返回以下内容:

ui.router

等等。

我目前的查询是:

| trip_id                         | service_id | departure_stop | departure_time | arrival_stop    | arrival_time | end_departure |
|---------------------------------|------------|----------------|----------------|-----------------|--------------|---------------|
| 15693832.T6.2-EPP-E-mjp-1.11.R  | T6_1       | Clifton Hill   | 04:56:00       | Flinders Street | 05:07:00     | 05:07:00      |

我试图弄清楚如何在select `t`.`trip_id`, `c`.`service_id`, `start_s`.`stop_name` as `departure_stop`, `start_st`.`departure_time`, `end_s`.`stop_name` as `arrival_stop`, `end_st`.`arrival_time`, `end_st`.`departure_time` as `end_departure` from `trips` as `t` inner join `calendar` as `c` on `t`.`service_id` = `c`.`service_id` inner join `routes` as `r` on `t`.`route_id` = `r`.`route_id` inner join `stop_times` as `start_st` on `t`.`trip_id` = `start_st`.`trip_id` inner join `stops` as `start_s` on `start_st`.`stop_id` = `start_s`.`stop_id` inner join `stop_times` as `end_st` on `t`.`trip_id` = `end_st`.`trip_id` inner join `stops` as `end_s` on `end_st`.`stop_id` = `end_s`.`stop_id` where `start_st`.`departure_time` > '00:00:00' and `start_st`.`departure_time` < '23:59:59' and `start_s`.`stop_id` = 19974 and `end_s`.`stop_id` = 19854 and start_st.departure_time < end_st.arrival_time order by arrival_time asc 表格中找到我的查询为每一行返回的stop_times的最后一站。

因此,除了我目前的情况,我还想:

  • 获取trip_id表格中stop_id的最后trip_id
  • stop_times表中获取相应stop_name
  • stops
  • 将其显示为输出中的最后一列stop_id

更新

我尝试选择last_stop并添加以下内部联接:

s.stop_name

但是,这会为旅行中的每个停靠点添加额外的行,其中我只想要最后一个,并且添加inner join ( SELECT s.stop_name, trip_id FROM stop_times INNER JOIN stops as s on `s`.`stop_id` = `stop_times`.`stop_id` ORDER BY stop_sequence DESC ) s on `t`.`trip_id` = `s`.`trip_id` 不会返回任何结果。

1 个答案:

答案 0 :(得分:2)

一个警告,到达和离开时间可能晚于GTFS规格的午夜(小时值可能是24,25等)

select "t"."trip_id",
       "c"."service_id",
       "start_s"."stop_name" as "departure_stop",
       "start_st"."departure_time",
       "end_s"."stop_name" as "arrival_stop",
       "end_st"."arrival_time",
       "end_st"."departure_time" as "end_departure",
       "last_st"."arrival_time" as "last_arrival",
       "last_s"."stop_name" as "last_stop"
from "trips" as "t"
    inner join "calendar" as "c" on "t"."service_id" = "c"."service_id"
    inner join "routes" as "r" on "t"."route_id" = "r"."route_id" 
    inner join "stop_times" as "start_st" on "t"."trip_id" = "start_st"."trip_id" 
    inner join "stops" as "start_s" on "start_st"."stop_id" = "start_s"."stop_id" 
    inner join "stop_times" as "end_st" on "t"."trip_id" = "end_st"."trip_id" 
    inner join "stops" as "end_s" on "end_st"."stop_id" = "end_s"."stop_id" 
    inner join "stop_times" as "last_st" on "t"."trip_id" = "last_st"."trip_id" 
    inner join "stops" as "last_s" on "last_st"."stop_id" = "last_s"."stop_id" 
where "start_s"."stop_id" = '245' -- my data's stop id
    and "end_s"."stop_id" = '762' -- my data's stop id
    and "last_st"."stop_sequence" = (select max("stop_sequence") from "stop_times" where "t"."trip_id" = "trip_id")
    and start_st.departure_time < end_st.arrival_time
order by arrival_time asc