| 220 | |
| 221 | * Извештај за топ 3 најпопуларни дестинации, аеродроми и летови за секој месец во годината |
| 222 | |
| 223 | {{{ |
| 224 | WITH MonthlyBookings AS ( |
| 225 | -- Count bookings for each destination per month |
| 226 | SELECT |
| 227 | DATE_TRUNC('month', b.bookingDate) AS month, |
| 228 | d.Name AS destination_name, |
| 229 | COUNT(b.BookingID) AS bookings_count, |
| 230 | RANK() OVER (PARTITION BY DATE_TRUNC('month', b.bookingDate) ORDER BY COUNT(b.BookingID) DESC) AS rank |
| 231 | FROM Booking b |
| 232 | JOIN Flight f ON b.FlightID = f.FlightID |
| 233 | JOIN Airport a ON f.ArrivalAirport = a.AirportID |
| 234 | JOIN Destination d ON a.City = d.DestinationID |
| 235 | GROUP BY month, destination_name |
| 236 | ), |
| 237 | |
| 238 | MonthlyFlights AS ( |
| 239 | -- Count bookings for each flight per month |
| 240 | SELECT |
| 241 | DATE_TRUNC('month', b.bookingDate) AS month, |
| 242 | f.FlightNumber, |
| 243 | COUNT(b.BookingID) AS bookings_count, |
| 244 | RANK() OVER (PARTITION BY DATE_TRUNC('month', b.bookingDate) ORDER BY COUNT(b.BookingID) DESC) AS rank |
| 245 | FROM Booking b |
| 246 | JOIN Flight f ON b.FlightID = f.FlightID |
| 247 | GROUP BY month, f.FlightNumber |
| 248 | ), |
| 249 | |
| 250 | MonthlyAirports AS ( |
| 251 | -- Count departures from each airport per month |
| 252 | SELECT |
| 253 | DATE_TRUNC('month', b.bookingDate) AS month, |
| 254 | a.Name AS airport_name, |
| 255 | COUNT(b.BookingID) AS departures_count, |
| 256 | RANK() OVER (PARTITION BY DATE_TRUNC('month', b.bookingDate) ORDER BY COUNT(b.BookingID) DESC) AS rank |
| 257 | FROM Booking b |
| 258 | JOIN Flight f ON b.FlightID = f.FlightID |
| 259 | JOIN Airport a ON f.DepartureAirport = a.AirportID |
| 260 | GROUP BY month, a.Name |
| 261 | ) |
| 262 | |
| 263 | -- Retrieve the top 3 destinations, flights, and airports for each month |
| 264 | SELECT month, 'Destination' AS category, destination_name AS name, bookings_count AS count FROM MonthlyBookings WHERE rank <= 3 |
| 265 | UNION ALL |
| 266 | SELECT month, 'Flight' AS category, FlightNumber AS name, bookings_count AS count FROM MonthlyFlights WHERE rank <= 3 |
| 267 | UNION ALL |
| 268 | SELECT month, 'Airport' AS category, airport_name AS name, departures_count AS count FROM MonthlyAirports WHERE rank <= 3 |
| 269 | ORDER BY month, category, count DESC; |
| 270 | }}} |