Route | Incident | delay_minute |
---|---|---|
63 | Operator | 60 |
63 | Operator | 24 |
63 | Mechanical | 89 |
54 | Operator | 70 |
54 | Sanitation | 34 |
54 | Operator | 12 |
From the example table above, I want to return one row per route with their most common type/form of Incident. Such that it would look like this:
Route | Incident |
---|---|
63 | Operator |
54 | Operator |
I have tried the following query, but I am unsure whether or not it returns the most common form of Incident per route:
SELECT Route
, Incident
FROM bus_delay;
I have also attempted to use COUNT(DISTINCT)
but I require the Incident type returned as string, per route:
SELECT DISTINCT Route
, Incident
, COUNT(Incident) AS count_incident
FROM bus_delay
GROUP BY Incident
, Route;
How do I query such a table to return one row per Route, with that row only showing the most common form of Incident for that Route?
3
Answers
Use
WINDOW
function andCTE
Try the following:
If there could be a ties in the Incident counts per route (multiple counts with the same value) and you want to return all of Incident with the maximum count then use
DENSE_RANK
instead ofROW_NUMBER
.See a demo.