简体   繁体   English

如何使用GTFS在午夜之后的今天出发时间中加入时间表?

[英]How can I include in schedules today's departures after midnight using GTFS?

I began with GTFS and offhand ran into big problem with my SQL query: 我从GTFS开始,但副手遇到了SQL查询的大问题:

SELECT *, ( some columns AS shortcuts )
FROM stop_times 
LEFT JOIN trips ON stop_times.trip_id = trips.trip_id
WHERE trips.max_sequence != stop_times.stop_sequence
AND stop_id IN( $incodes )
AND trips.service_id IN ( $service_ids )
AND ( departure_time >= $time )
AND ( trips.end_time >= $time )
AND ( trips.start_time <= $time_plus_3hrs )
GROUP BY t,l,sm
ORDER BY t ASC, l DESC
LIMIT 14

This should show departures from some stop in next 3 hours. 这应显示接下来3小时内某些站点的出发时间。 It works but with approaching midnight (eg 23:50) it catch only "today's departure". 它可以正常工作,但是随着临近午夜(例如23:50),它只能捕获“今天的离开”。 After midnight it catch only "new day departures" and departures from previous day are missing, because they have departure_time eg "24:05" (=not bigger than $time 00:05). 午夜之后,它仅捕获“新的一天出发时间”,并且缺少前一天的出发时间,因为它们具有“ department_time”,例如“ 24:05”(=不大于$ time 00:05)。 Is possible to use something lighter than UNION same query for next day? 第二天是否可以使用比UNION同一查询更轻的内容? If UNION is using, how can I ORDER departures for trimming by LIMIT? 如果使用的是UNION,我如何按LIMIT顺序排序偏差?

Trips.start_time and end_time are my auxiliary variables for accelerate SQL query execution, it means sequence1-arrival_time and MAXsequence-departure_time of any trip. Trips.start_time和end_time是我用于加速SQL查询执行的辅助变量,它表示任何行程的sequence1-arrival_time和MAXsequence-departure_time。

Using UNION to link together a query for each day is going to be your best bet, unless perhaps you want to issue two completely separate queries and then merge the results together in your application. 最好每天使用UNION将每天的查询链接在一起,除非您可能想发出两个完全独立的查询,然后将结果合并到应用程序中。 The contortionism required to do all this with a single SELECT statement (assuming it's even possible) would not be worth the effort. 用单个SELECT语句(假设甚至可能)来完成所有这些操作,这是不值得的。

Part of the complexity here is that the set of active service IDs can vary between consecutive days, so a distinct set must be used for each one. 复杂性的部分原因在于,活动服务ID的集合在连续的几天之间可能会有所不同,因此必须为每个服务ID使用不同的集合。 (For a suggestion of how to build this set in SQL using a subquery and table join, see my answer to "How do I use calendar exceptions to generate accurate schedules using GTFS?" .) (有关如何使用子查询和表联接在SQL中构建此集合的建议,请参阅我对“如何使用日历异常使用GTFS生成准确的计划?”的回答 。)

More complexity arises from the fact the results for each day must be treated differently: For the result set to be ordered correctly, we need to subtract twenty-four hours from all of (and only) yesterday's times. 每天的结果必须以不同的方式处理会带来更多的复杂性:为了正确排序结果集,我们需要从昨天的所有(且仅)时间中减去24小时。

Try a query like this, following the "pseudo-SQL" in your question and assuming you are using MySQL/MariaDB: 尝试这样的查询,遵循问题中的“伪SQL”,并假设您正在使用MySQL / MariaDB:

SELECT *, SUBTIME(departure_time, '24:00:00') AS t, ...
  FROM stop_times
  LEFT JOIN trips ON stop_times.trip_id = trips.trip_id
  WHERE trips.max_sequence != stop_times.stop_sequence
    AND stop_id IN ( $incodes )
    AND trips.service_id IN ( $yesterdays_service_ids )
    AND ( departure_time >= ADDTIME($time, '24:00:00') )
    AND ( trips.end_time >= ADDTIME($time, '24:00:00') )
    AND ( trips.start_time <= ADDTIME($time_plus_3hrs, '24:00:00') )
  UNION
    SELECT *, departure_time AS t, ...
      FROM stop_times 
      LEFT JOIN trips ON stop_times.trip_id = trips.trip_id
      WHERE trips.max_sequence != stop_times.stop_sequence
        AND stop_id IN ( $incodes )
        AND trips.service_id IN ( $todays_service_ids )
        AND ( departure_time >= $time )
        AND ( trips.end_time >= $time )
        AND ( trips.start_time <= $time_plus_3hrs )
  GROUP BY t, l, sm
  ORDER BY t ASC, l DESC
  LIMIT 14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM