Sponsored
Sponsored
This approach involves using SQL joins between the 'Trips' and 'Users' tables to filter out trips involving banned users. Then, group the results by date and count the total and cancelled trips to compute the cancellation rate for each day.
Time Complexity: Not applicable
Space Complexity: Not applicable
1import sqlite3
2
3# Assuming connection and tables setup
4connection = sqlite3.connect(':memory:')
5cursor = connection.cursor()
6
7query = '''
8SELECT t.request_at AS Day,
9 ROUND(SUM(CASE WHEN t.status != 'completed' THEN 1 ELSE 0 END) * 1.0 / COUNT(*), 2) AS CancellationRate
10FROM Trips t
11JOIN Users c ON t.client_id = c.users_id AND c.banned = 'No'
12JOIN Users d ON t.driver_id = d.users_id AND d.banned = 'No'
13WHERE t.request_at BETWEEN '2013-10-01' AND '2013-10-03'
14GROUP BY t.request_at;
15'''
16
17cursor.execute(query)
18results = cursor.fetchall()
19for row in results:
20 print(f"{row[0]}: {row[1]}")
This Python code snippet assumes you're working with an SQLite database directly. It runs a SQL query to find trips between the specified dates where neither client nor driver is banned, calculates the cancellation rate, and rounds it to two decimal places.
This approach involves extracting trip data and user data, performing filtering operations in the application logic to compute stats, and then using aggregation to calculate the cancellation rate for each day.
Time Complexity: Not applicable
Space Complexity: Not applicable
1// Java code is not applicable for database queries.
Java would normally use JDBC or an ORM for such operations. Pure Java alone is not applicable here.