I have a some data in the following format:
| Bus | Route |
|---|---|
| Slowcoach | SC123 |
| Slowcoach | SC123 |
| Slowcoach | SC123 |
| Slowcoach | SC555 |
| Slowcoach | SC555 |
| Slowcoach | SC555 |
| Slowcoach | SC555 |
| Slowcoach | SC111 |
| SpeedyTram | ST111 |
| SpeedyTram | ST111 |
| SpeedyTram | ST222 |
| SpeedyTram | ST222 |
| SpeedyTram | ST222 |
| SpeedyTram | ST222 |
| SpeedyTram | ST333 |
| SpeedyTram | ST444 |
I want to count the Routes and then show only the most popular 2, grouped by Bus :
| Bus | Route | Count |
|---|---|---|
| Slowcoach | SC555 | 4 |
| Slowcoach | SC123 | 3 |
| SpeedyTram | ST222 | 4 |
| SpeedyTram | ST111 | 2 |
I have the following so far:
SELECT Bus, Route, COUNT(Route)
FROM my_table
GROUP BY Bus, Route
ORDER BY Bus, COUNT DESC
I have looked at Rank / Partition /Limit but I can't get the COUNT field to work, nor can I work out the correct syntax.
Group and count (
ti) then order/assign position by bus (tx) and select these with position <= 2.SQL fiddle
The same using
tiandtxas CTEs, maybe cleaner and more readable:Edit
If you need the result with ties then use
dense_rank()instead ofrow_number().