Calculate Parking Fees and Duration - Problem
๐ Smart Parking Analytics System
You're working for a smart parking management company that operates multiple parking lots across the city. Your task is to analyze parking transaction data to generate comprehensive insights for each vehicle.
Given a ParkingTransactions table with parking records, you need to calculate:
- Total fees paid by each car across all parking lots
- Average hourly rate each car pays (rounded to 2 decimal places)
- Preferred parking lot where each car spends the most total time
This analysis helps the company understand customer behavior, optimize pricing strategies, and identify the most popular parking locations.
Note: A car cannot be in multiple parking lots simultaneously, ensuring data integrity.
Input & Output
basic_parking_analysis.sql โ Basic Example
$
Input:
ParkingTransactions:
+--------+--------+---------------------+---------------------+----------+
| lot_id | car_id | entry_time | exit_time | fee_paid |
+--------+--------+---------------------+---------------------+----------+
| 1 | 1001 | 2023-06-01 08:00:00 | 2023-06-01 10:30:00 | 5.00 |
| 1 | 1001 | 2023-06-02 11:00:00 | 2023-06-02 12:45:00 | 3.00 |
| 2 | 1001 | 2023-06-01 10:45:00 | 2023-06-01 12:00:00 | 6.00 |
| 2 | 1002 | 2023-06-01 09:00:00 | 2023-06-01 11:30:00 | 4.00 |
| 3 | 1001 | 2023-06-03 07:00:00 | 2023-06-03 09:00:00 | 4.00 |
| 3 | 1002 | 2023-06-02 12:00:00 | 2023-06-02 14: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 |
+--------+----------------+----------------+---------------+
๐ก Note:
Car 1001 paid total $18.00 across 7.5 hours ($2.40/hour average), spending most time (4.25 hours) in lot 1. Car 1002 paid $6.00 across 4.5 hours ($1.33/hour average), spending most time (2.5 hours) in lot 2.
single_car_multiple_lots.sql โ Multiple Lots
$
Input:
ParkingTransactions:
+--------+--------+---------------------+---------------------+----------+
| lot_id | car_id | entry_time | exit_time | fee_paid |
+--------+--------+---------------------+---------------------+----------+
| 1 | 2001 | 2023-06-01 09:00:00 | 2023-06-01 11:00:00 | 4.00 |
| 2 | 2001 | 2023-06-01 14:00:00 | 2023-06-01 17:00:00 | 9.00 |
| 3 | 2001 | 2023-06-02 10:00:00 | 2023-06-02 12:00:00 | 8.00 |
+--------+--------+---------------------+---------------------+----------+
โบ
Output:
+--------+----------------+----------------+---------------+
| car_id | total_fee_paid | avg_hourly_fee | most_time_lot |
+--------+----------------+----------------+---------------+
| 2001 | 21.00 | 3.00 | 2 |
+--------+--------+----------------+----------------+---------------+
๐ก Note:
Car 2001 used 3 different lots: 2 hours in lot 1 ($4), 3 hours in lot 2 ($9), 2 hours in lot 3 ($8). Total: $21 over 7 hours = $3.00/hour. Most time spent in lot 2 (3 hours).
tie_breaking_lots.sql โ Lot Tie Breaking
$
Input:
ParkingTransactions:
+--------+--------+---------------------+---------------------+----------+
| lot_id | car_id | entry_time | exit_time | fee_paid |
+--------+--------+---------------------+---------------------+----------+
| 1 | 3001 | 2023-06-01 09:00:00 | 2023-06-01 11:00:00 | 6.00 |
| 2 | 3001 | 2023-06-01 14:00:00 | 2023-06-01 16:00:00 | 4.00 |
+--------+--------+---------------------+---------------------+----------+
โบ
Output:
+--------+----------------+----------------+---------------+
| car_id | total_fee_paid | avg_hourly_fee | most_time_lot |
+--------+----------------+----------------+---------------+
| 3001 | 10.00 | 2.50 | 1 |
+--------+----------------+----------------+---------------+
๐ก Note:
Car 3001 spent equal time (2 hours each) in lots 1 and 2. When there's a tie in hours, the smaller lot_id (1) is chosen as the most_time_lot. Total: $10 over 4 hours = $2.50/hour.
Constraints
- 1 โค Number of transactions โค 105
- 1 โค lot_id โค 100
- 1000 โค car_id โค 9999
- entry_time < exit_time for all transactions
- 0.01 โค fee_paid โค 999.99
- Maximum parking duration per transaction: 24 hours
- All timestamps are in format: YYYY-MM-DD HH:MM:SS
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Each parking transaction is recorded with entry/exit times and fees
2
Duration Calculation
Calculate hours parked using time difference (like calculating shopping time)
3
Aggregation
Group by car to sum total fees and hours (like monthly expense summary)
4
Analysis
Find average rate and preferred location (like finding your most visited store)
Key Takeaway
๐ฏ Key Insight: Group parking transactions by car_id and use window functions to rank lots by time spent, enabling efficient calculation of all required metrics in a single database query.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code