Changes between Version 3 and Version 4 of AnalyticalStatisticalQuerying


Ignore:
Timestamp:
08/27/25 16:02:00 (6 days ago)
Author:
221507
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AnalyticalStatisticalQuerying

    v3 v4  
    118118
    119119== 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.
     120
     121This SQL query aggregates ticket data by destination over the past year, calculating total passengers, unique customers, revenue, and specific ticket types, then returns destinations with passenger counts at or above the average, ordered by total passengers in descending order.
     122
    121123{{{
     124
     125WITH destinations AS (
     126    SELECT
     127        l.name AS destination,
     128        COUNT(t.ticket_id) AS total_passengers,
     129        COUNT(DISTINCT t.account_id) AS unique_customers,
     130        SUM(t.price) AS total_revenue,
     131        COUNT(st.student_ticket_id) AS student_tickets,
     132        COUNT(ct.child_ticket_id) AS child_tickets,
     133        l.latitude,
     134        l.longitude
     135    FROM location l
     136    JOIN ticket t ON l.location_id = t.gets_off_location_id
     137    JOIN trip tr ON t.trip_id = tr.trip_id
     138    JOIN transport_organizer to_org ON tr.transport_organizer_id = to_org.transport_organizer_id
     139    LEFT JOIN student_ticket st ON t.ticket_id = st.ticket_id
     140    LEFT JOIN child_ticket ct ON t.ticket_id = ct.ticket_id
     141    GROUP BY l.location_id, l.name, l.latitude, l.longitude
     142),
     143stats AS (
     144    SELECT AVG(total_passengers) AS mean_passengers -- this is used to avoid hard limits
     145    FROM destinations
     146)
    122147SELECT
    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;
     148    d.destination,
     149    d.total_passengers,
     150    d.unique_customers,
     151    d.total_revenue,
     152    d.student_tickets,
     153    d.child_tickets
     154FROM destinations d, stats
     155WHERE d.total_passengers >= stats.mean_passengers
     156ORDER BY d.total_passengers DESC;
     157
    139158}}}
    140159
     
    144163This 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.
    145164{{{
    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;
     165WITH route_stats AS (
     166    SELECT
     167        r.route_id,
     168        r.from_location_id,
     169        r.to_location_id,
     170        COUNT(tk.ticket_id) AS total_tickets_sold,
     171        SUM(p.total_price) AS total_revenue,
     172        AVG(p.total_price) AS avg_ticket_price
     173    FROM route r
     174    JOIN trip tr ON r.route_id = tr.route_id
     175    JOIN ticket tk ON tr.trip_id = tk.trip_id
     176    JOIN payment p ON tk.payment_id = p.payment_id
     177    WHERE r.transport_organizer_id = 100 -- just for reference
     178    GROUP BY r.route_id, r.from_location_id, r.to_location_id
     179)
     180SELECT rs.*
     181FROM route_stats rs
     182WHERE rs.total_tickets_sold >= 0.8 *
     183        (SELECT
     184             MAX(total_tickets_sold) FROM route_stats) -- this is used to retrieve top selling tickets, the percentage is flexible
     185ORDER BY rs.total_tickets_sold DESC;
     186
    161187}}}
    162188