From my train schedule database I want to get trains overtaken by train 12301. What would the SQL query be?
Overtake can be at a station (train A arrives and departs in between arrival and departure of train B) or between stations (A departs from station 1 at 2 pm, B at 2:10 pm, and A arrives at station 2 at 2:30 pm, B at 2:25 pm), but only in the same direction because despite that train 12301 (going 1-2-3-4-5) and train 12232 (going 50-23-3-22-21) meet at station 3 and their arrival and departure times match, it isn’t an overtake (they are going opposite ways).
data class Schedules(
val number: String="",
val stationCode: String="",
val type: Int=0,
val arrTime: Int=0,
val depTime: Int=0,
val dayNum: Int=0,
val km: Int=0
)
Table and data:
CREATE TABLE train_schedules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
number TEXT NOT NULL,
stationCode TEXT NOT NULL,
type INTEGER NOT NULL,
arrTime INTEGER NOT NULL,
depTime INTEGER NOT NULL,
dayNum INTEGER NOT NULL,
km INTEGER NOT NULL
);
-- Train 12301 (our reference train)
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12301', 'STA', 1, 0, 10, 1, 0),
('12301', 'STB', 0, 60, 60, 1, 50),
('12301', 'STC', 1, 120, 122, 1, 100),
('12301', 'STD', 1, 180, 182, 1, 150),
('12301', 'STE', 1, 240, 242, 1, 200),
('12301', 'STF', 0, 300, 300, 1, 250),
('12301', 'STG', 1, 355, 365, 1, 300),
('12301', 'STH', 0, 420, 420, 1, 350),
('12301', 'STI', 1, 480, 482, 1, 400),
('12301', 'STJ', 1, 540, 0, 1, 450);
-- Train 12302 (faster than 12301)
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12302', 'STA', 1, 30, 32, 1, 0),
('12302', 'STB', 0, 80, 80, 1, 50),
('12302', 'STC', 1, 130, 132, 1, 100),
('12302', 'STD', 0, 181, 181, 1, 150),
('12302', 'STE', 1, 230, 232, 1, 200),
('12302', 'STF', 0, 280, 280, 1, 250),
('12302', 'STG', 1, 330, 332, 1, 300),
('12302', 'STH', 0, 380, 380, 1, 350),
('12302', 'STI', 1, 430, 432, 1, 400),
('12302', 'STJ', 1, 480, 0, 1, 450);
-- Train 12303 (slower than 12301)
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12303', 'STA', 1, 0, 5, 1, 0),
('12303', 'STB', 0, 70, 70, 1, 50),
('12303', 'STC', 1, 140, 142, 1, 100),
('12303', 'STD', 0, 210, 210, 1, 150),
('12303', 'STE', 1, 280, 282, 1, 200),
('12303', 'STF', 0, 350, 350, 1, 250),
('12303', 'STG', 1, 420, 422, 1, 300),
('12303', 'STH', 0, 490, 490, 1, 350),
('12303', 'STI', 1, 560, 562, 1, 400),
('12303', 'STJ', 1, 630, 0, 1, 450);
-- Train 12304 (starts later, overtakes 12301)
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12304', 'STA', 1, 60, 62, 1, 0),
('12304', 'STB', 0, 110, 110, 1, 50),
('12304', 'STC', 1, 160, 162, 1, 100),
('12304', 'STD', 0, 210, 210, 1, 150),
('12304', 'STE', 1, 260, 262, 1, 200),
('12304', 'STF', 0, 310, 310, 1, 250),
('12304', 'STG', 1, 360, 362, 1, 300),
('12304', 'STH', 0, 410, 410, 1, 350),
('12304', 'STI', 1, 460, 462, 1, 400),
('12304', 'STJ', 1, 510, 0, 1, 450);
-- Train 12305 (similar speed to 12301, different stops)
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12305', 'STA', 1, 10, 12, 1, 0),
('12305', 'STB', 1, 70, 72, 1, 50),
('12305', 'STC', 0, 130, 130, 1, 100),
('12305', 'STD', 1, 190, 192, 1, 150),
('12305', 'STE', 0, 250, 250, 1, 200),
('12305', 'STF', 1, 310, 312, 1, 250),
('12305', 'STG', 0, 370, 370, 1, 300),
('12305', 'STH', 1, 430, 432, 1, 350),
('12305', 'STI', 0, 490, 490, 1, 400),
('12305', 'STJ', 1, 550, 0, 1, 450);
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12306', 'STJ', 1, 10, 12, 1, 0),
('12306', 'STI', 0, 35, 35, 1, 50),
('12306', 'STH', 1, 70, 72, 1, 100),
('12306', 'STF', 1, 130, 132, 1, 200),
('12306', 'STD', 1, 190, 192, 1, 300),
('12306', 'STB', 1, 250, 252, 1, 400),
('12306', 'STA', 1, 310, 0, 1, 450);
--going opposite way of 12301
INSERT INTO train_schedules (number, stationCode, type, arrTime, depTime, dayNum, km) VALUES
('12307', 'STJ', 1, 0, 0, 1, 0),
('12307', 'STI', 0, 60, 60, 1, 50),
('12307', 'STH', 1, 120, 122, 1, 100),
('12307', 'STG', 1, 180, 182, 1, 150),
('12307', 'STF', 1, 300, 305, 1, 200),
('12307', 'STE', 0, 320, 320, 1, 250),
('12307', 'STD', 1, 355, 365, 1, 300),
('12307', 'STC', 0, 420, 420, 1, 350),
('12307', 'STB', 1, 480, 482, 1, 400),
('12307', 'STA', 1, 540, 0, 1, 450);
Desired Result-
First-
How could I get trains sharing the route with 12301, at least 2 consecutive stations(like STA-STB, STC-STD-STE, Not STA-STC, not STE-STG)?
Second –
If I query train 12301 it should tell trains overtake 12301 / overtaken by 12301.
The result should have train number(overtaken/overtakes), statCode(where overtake happened), and time.
Overtakes 12303 between STA and STB,
Overtaken by 12302 at STD on 181,
Overtaken by 12304 at STG on 362
12307 at STF is not overtaken because she is going the opposite way.
3