All problems · Windows
Codes covering 80 percent
Sort positive-total delay codes by summed delay descending and code ascending; report the smallest number whose cumulative minutes reach at least 80% of all recorded delay minutes, and the last code needed.
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 c AS
(SELECT code,
sum(delay) AS minutes
FROM incident
GROUP BY code
HAVING sum(delay)>0),
r AS
(SELECT code,
row_number() OVER w AS codes,
sum(minutes) OVER w AS cumulative,
sum(minutes) OVER () AS total
FROM c WINDOW w AS (
ORDER BY minutes DESC,
code ROWS UNBOUNDED PRECEDING))
SELECT codes,
code AS crossing_code
FROM r
WHERE 5*cumulative>=4*total
ORDER BY codes
LIMIT 1;
Expected output
| codes | crossing_code |
|---|---|
| 25 | SUG |