I am trying to get a result (there should be only one) on an SQLite query using a subquery. As I am pretty new to this I am having a problem.
Given a stop_name in the stops table, and an arrival_time from the stop_times table......extract the departure time at at a different stop_name.
Short of a rip in the space time continuum, even the fastest train, does not leave after it arrives.....as my query would suggest.
Here is the schema of the database:
CREATE TABLE trips (
_id INTEGER PRIMARY KEY,
block_id TEXT,
route_id TEXT,
direction_id TEXT,
trip_headsign TEXT,
shape_id TEXT,
service_id TEXT,
trip_id TEXT,
FOREIGN KEY(route_id) REFERENCES routes(route_id),
FOREIGN KEY(service_id) REFERENCES calendar(service_id)
);
CREATE TABLE calendar(
_id INTEGER PRIMARY KEY,
service_id TEXT,
start_date TEXT,
end_date TEXT,
monday TEXT,
tuesday TEXT,
wednesday TEXT,
thursday TEXT,
friday TEXT,
saturday TEXT,
sunday TEXT
);
CREATE TABLE routes (
_id INTEGER PRIMARY KEY,
route_long_name TEXT,
route_type TEXT,
route_text_color TEXT,
agency_id TEXT,
route_id TEXT,
route_color TEXT,
route_short_name TEXT
);
CREATE TABLE stops (
_id INTEGER PRIMARY KEY,
stop_lat DECIMAL(9,6),
zone_id TEXT,
stop_lon DECIMAL(9,6),
stop_id TEXT,
stop_desc TEXT,
stop_name TEXT,
location_type TEXT,
stop_code TEXT
);
CREATE TABLE stop_times (
_id INTEGER PRIMARY KEY,
trip_id TEXT,
arrival_time TEXT,
departure_time TEXT,
stop_id TEXT,
stop_sequence TEXT,
stop_headsign TEXT,
pickup_type TEXT,
drop_off_type TEXT,
shape_dist_traveled TEXT,
timepoint TEXT,
FOREIGN KEY(stop_id) REFERENCES stops(stop_id),
FOREIGN KEY(trip_id) REFERENCES trips(trip_id)
);
CREATE TABLE android_metadata (
locale TEXT
);
The subquery separately gives me a count of 50 unique trip_ids which I believe is needed to get the correct departure time.
Here is the query that gives me the non-Einsteinian output of 20:19:00.
SELECT st.departure_time FROM routes r
JOIN trips t ON t.route_id = r.route_id
JOIN calendar c ON c.service_id =t.service_id
JOIN stop_times st ON st.trip_id = t.trip_id
JOIN stops s ON s.stop_id = st.stop_id
WHERE s.stop_name LIKE "STADIUM%"
AND st.trip_id = (SELECT t.trip_id FROM routes r
JOIN trips t ON t.route_id = r.route_id
JOIN calendar c ON c.service_id =t.service_id
JOIN stop_times st ON st.trip_id = t.trip_id
JOIN stops s ON s.stop_id = st.stop_id
WHERE r.route_id LIKE "12594R"
AND s.stop_name LIKE "LAMBERT EAST TRML METROLINK STATION"
AND t.direction_id = "0"
AND st.arrival_time >= "11:00:00"
AND c.monday = "1");
You may not be able to figure this out with out the database. If any one would like to f*** with this I have pushed it to
Feel free to fork it and mess with it as you please.
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire