+--------------+-----------+
|Column Name |Type|+--------------+-----------+
| lot_id | int || car_id | int || entry_time | datetime || exit_time | datetime || fee_paid | decimal |+--------------+-----------+
(lot_id, car_id, entry_time) is the primarykey (combination of columns withuniquevalues) for this table.
Eachrowof this tablecontains the ID of the parking lot, the ID of the car, the entry and exit times, and the fee paid for the parking duration.
Write a solution to find the total parking fee paid by each car across all parking lots , and the average hourly fee (rounded to 2 decimal places) paid by each car. Also, find the parking lot where each car spent the most total time.
Return the result table ordered bycar_id**** inascendingorder.
Note: Test cases are generated in such a way that an individual car cannot be in multiple parking lots at the same time.
Input:
ParkingTransactions table:+--------+--------+---------------------+---------------------+----------+| lot_id | car_id | entry_time | exit_time | fee_paid |+--------+--------+---------------------+---------------------+----------+|1|1001|2023-06-0108:00:00|2023-06-0110:30:00|5.00||1|1001|2023-06-0211:00:00|2023-06-0212:45:00|3.00||2|1001|2023-06-0110:45:00|2023-06-0112:00:00|6.00||2|1002|2023-06-0109:00:00|2023-06-0111:30:00|4.00||3|1001|2023-06-0307:00:00|2023-06-0309:00:00|4.00||3|1002|2023-06-0212:00:00|2023-06-0214:00:00|2.00|+--------+--------+---------------------+---------------------+----------+Output:
+--------+----------------+----------------+---------------+| car_id | total_fee_paid | avg_hourly_fee | most_time_lot |+--------+----------------+----------------+---------------+|1001|18.00|2.40|1||1002|6.00|1.33|2|+--------+----------------+----------------+---------------+Explanation:
* For car ID 1001:* From 2023-06-0108:00:00 to 2023-06-0110:30:00in lot 1:2.5 hours, fee 5.00* From 2023-06-0211:00:00 to 2023-06-0212:45:00in lot 1:1.75 hours, fee 3.00* From 2023-06-0110:45:00 to 2023-06-0112:00:00in lot 2:1.25 hours, fee 6.00* From 2023-06-0307:00:00 to 2023-06-0309:00:00in lot 3:2 hours, fee 4.00Total fee paid:18.00, total hours:7.5, average hourly fee:2.40, most time
spent in lot 1:4.25 hours.* For car ID 1002:* From 2023-06-0109:00:00 to 2023-06-0111:30:00in lot 2:2.5 hours, fee 4.00* From 2023-06-0212:00:00 to 2023-06-0214:00:00in lot 3:2 hours, fee 2.00Total fee paid:6.00, total hours:4.5, average hourly fee:1.33, most time
spent in lot 2:2.5 hours.**Note:** Output table is ordered by car_id in ascending order.
We need to aggregate data per car: total fee, average hourly fee, and the lot with the most time spent. We use SUM for total fee, calculate total hours for average fee, and use window functions to find the lot with the maximum total time for each car.
WITH durations AS (
SELECT car_id,
lot_id,
fee_paid,
TIMESTAMPDIFF(MINUTE, entry_time, exit_time) /60AS hours
FROM ParkingTransactions
),
car_agg AS (
SELECT car_id,
ROUND(SUM(fee_paid), 2) AS total_fee,
SUM(hours) AS total_hours,
ROUND(SUM(fee_paid) /NULLIF(SUM(hours), 0), 2) AS average_hourly_fee
FROM durations
GROUPBY car_id
),
lot_time AS (
SELECT car_id,
lot_id,
SUM(hours) AS lot_hours,
ROW_NUMBER() OVER (PARTITION BY car_id ORDERBYSUM(hours) DESC, lot_id ASC) AS rn
FROM durations
GROUPBY car_id, lot_id
)
SELECTc.car_id,
c.total_fee,
c.average_hourly_fee,
l.lot_id AS most_time_lot
FROM car_agg cJOIN lot_time l ONc.car_id = l.car_id AND l.rn =1ORDERBYc.car_id ASC;