All problems · Windows
Daily station alert versus prior observations
For each station, aggregate delay per observed date and compare each total to the mean of its prior seven observed dates; show the five largest positive excesses where all seven exist, breaking ties by station then date and rounding excess to two decimals.
Source
Data: TTC Subway Delay Data (City of Toronto, 2023 snapshot), licence, read 2026-11-29.
Schema
incident(id INTEGER, service_date TEXT, clock_time TEXT, weekday TEXT, station TEXT, code TEXT, delay INTEGER, gap INTEGER, bound TEXT, line TEXT, vehicle INTEGER); delay_code(code TEXT, description TEXT). One incident row is one workbook record, not necessarily a unique event. Dates are ISO YYYY-MM-DD; id is the 1-based data row ordinal. Delay and gap are recorded minutes; gap is time between trains. Blank bound and line values remain NULL; zero values and raw labels are preserved. IDs identify source rows, not riders. Contains information licensed under the Open Government Licence – Toronto.
Show the checked solution
SQL
WITH d AS
(SELECT station,
service_date,
sum(delay) AS minutes
FROM incident
GROUP BY station,
service_date),
r AS
(SELECT *,
avg(minutes) OVER w AS baseline,
count(*) OVER w AS n
FROM d WINDOW w AS (PARTITION BY station
ORDER BY service_date ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING))
SELECT station,
service_date,
minutes,
round(minutes-baseline, 2) AS excess
FROM r
WHERE n=7
AND minutes>baseline
ORDER BY minutes-baseline DESC,
station,
service_date
LIMIT 5;
Expected output
| station | service_date | minutes | excess |
|---|---|---|---|
| ELLESMERE STATION | 2023-07-24 | 423 | 418.57 |
| LAWRENCE EAST STATION | 2023-04-16 | 420 | 412.14 |
| BLOOR STATION | 2023-04-25 | 360 | 358 |
| MCCOWAN STATION | 2023-06-18 | 325 | 317.57 |
| MCCOWAN STATION | 2023-02-03 | 314 | 307.57 |