| 118 | |
| 119 | == 5. Most popular destinations in the past year == |
| 120 | This SQL query retrieves the top 5 most popular destinations from the last year, showing passenger volume, revenue, and breakdown of student and child ticket sales for each location. |
| 121 | {{{ |
| 122 | SELECT |
| 123 | l.name AS destination, |
| 124 | COUNT(t.ticket_id) AS total_passengers, |
| 125 | COUNT(DISTINCT t.account_id) AS unique_customers, |
| 126 | SUM(t.price) AS total_revenue, |
| 127 | COUNT(st.student_ticket_id) AS student_tickets, |
| 128 | COUNT(ct.child_ticket_id) AS child_tickets |
| 129 | FROM location l |
| 130 | JOIN ticket t ON l.location_id = t.gets_off_location_id |
| 131 | JOIN trip tr ON t.trip_id = tr.trip_id |
| 132 | JOIN transport_organizer TO_id ON tr.transport_organizer_id = TO_id.transport_organizer_id |
| 133 | LEFT JOIN student_ticket st ON t.ticket_id = st.ticket_id |
| 134 | LEFT JOIN child_ticket ct ON t.ticket_id = ct.ticket_id |
| 135 | WHERE t.date_purchased >= CURRENT_DATE - INTERVAL '1 year' |
| 136 | GROUP BY l.location_id, l.name, l.latitude, l.longitude |
| 137 | ORDER BY total_passengers DESC |
| 138 | LIMIT 5; |
| 139 | }}} |
| 140 | |