All problems · Joins
Distinguish zero logs from missing combinations
Build all 12 calendar months for the three most frequently reported station labels (ties alphabetical); return the count of station-month cells without a positive-delay row for each label.
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 RECURSIVE m(month) AS
(SELECT '2023-01-01'
UNION ALL SELECT date(month, '+1 month')
FROM m
WHERE month<'2023-12-01'),
s AS
(SELECT station
FROM incident
GROUP BY station
ORDER BY count(*) DESC,station
LIMIT 3),
p AS
(SELECT DISTINCT station,
substr(service_date, 1, 7) AS month
FROM incident
WHERE delay>0)
SELECT s.station,
count(*)-count(p.month) AS empty_months
FROM s
CROSS JOIN m
LEFT JOIN p ON p.station=s.station
AND p.month=substr(m.month, 1, 7)
GROUP BY s.station
ORDER BY s.station;
Expected output
| station | empty_months |
|---|---|
| BLOOR STATION | 0 |
| DUNDAS STATION | 0 |
| KENNEDY BD STATION | 0 |