Data Interview Prep
Problems are free. Session requests and mentor applications are saved and the owner follows up by email. Nothing is charged on this site.

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

stationservice_dateminutesexcess
ELLESMERE STATION2023-07-24423418.57
LAWRENCE EAST STATION2023-04-16420412.14
BLOOR STATION2023-04-25360358
MCCOWAN STATION2023-06-18325317.57
MCCOWAN STATION2023-02-03314307.57

Practise this with a mentor