Changes between Version 1 and Version 2 of Напредни извештаи од базата (SQL и складирани процедури)


Ignore:
Timestamp:
02/06/25 14:07:32 (2 weeks ago)
Author:
173067
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Напредни извештаи од базата (SQL и складирани процедури)

    v1 v2  
    218218
    219219}}}
     220
     221* Извештај за топ 3 најпопуларни дестинации, аеродроми и летови за секој месец во годината
     222
     223{{{
     224WITH 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
     238MonthlyFlights 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
     250MonthlyAirports 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
     264SELECT month, 'Destination' AS category, destination_name AS name, bookings_count AS count FROM MonthlyBookings WHERE rank <= 3
     265UNION ALL
     266SELECT month, 'Flight' AS category, FlightNumber AS name, bookings_count AS count FROM MonthlyFlights WHERE rank <= 3
     267UNION ALL
     268SELECT month, 'Airport' AS category, airport_name AS name, departures_count AS count FROM MonthlyAirports WHERE rank <= 3
     269ORDER BY month, category, count DESC;
     270}}}