| | 141 | |
| | 142 | |
| | 143 | == 6. Top selling routes for transport organizers == |
| | 144 | This query retrieves the most popular routes for a specific transport organizer, ranking them by the number of tickets sold while also showing total revenue and average ticket price. |
| | 145 | {{{ |
| | 146 | SELECT |
| | 147 | r.route_id, |
| | 148 | r.from_location_id, |
| | 149 | r.to_location_id, |
| | 150 | COUNT(tk.ticket_id) AS total_tickets_sold, |
| | 151 | SUM(p.total_price) AS total_revenue, |
| | 152 | AVG(p.total_price) AS avg_ticket_price |
| | 153 | FROM route r |
| | 154 | JOIN trip tr ON r.route_id = tr.route_id |
| | 155 | JOIN ticket tk ON tr.trip_id = tk.trip_id |
| | 156 | JOIN payment p ON tk.payment_id = p.payment_id |
| | 157 | WHERE r.transport_organizer_id = 100 -- just for reference |
| | 158 | GROUP BY r.route_id, r.from_location_id, r.to_location_id |
| | 159 | ORDER BY total_tickets_sold DESC |
| | 160 | LIMIT 5; |
| | 161 | }}} |
| | 162 | |
| | 163 | |