mysql - GTFS 获得旅行的最后一站

标签 mysql sql gtfs

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

| 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_times 表中获取该 trip_id 的最后一个 stop_id
  • stops 表中获取相应 stop_idstop_name
  • 将其显示为输出中的最后一列 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`

但是,这会为行程中的每一站添加额外的行,我只想要最后一站,添加 LIMIT 1 不会返回任何结果。

最佳答案

请注意,到达和离开时间可能晚于 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

关于mysql - GTFS 获得旅行的最后一站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32327925/

相关文章:

mysql - 如何查找某个点附近的路线

sql - 如何从 INSERT INTO SELECT FROM 查询中检索新键?

postgresql - 如何从单个字段中的多个值进行搜索(GTFS 中的公交车站)?

mysql - 根据同一表中的其他行更新行

mysql - Case 语句 Mysql 查询根据条件加载数据

sql - 初始化pl/sql记录类型

sql - 根据列 id(顺序)将所选数据合并到同一行

Android 使用 GTFS-Realtime(无 API)

php - 上传多个文件并将路径保存到数据库时出现问题

mysql - Rails 统计记录而无需再次查询