Skip to main content

LeetCode 262. Trips and Users SQL Solution

Problem

LeetCode SQL Problem

  1. Trips and Users

Trips table

idclient_iddriver_idcity_idstatusrequest_at
11101completed2013-10-01T00:00:00Z
22111cancelled_by_driver2013-10-01T00:00:00Z
33126completed2013-10-01T00:00:00Z
44136cancelled_by_client2013-10-01T00:00:00Z
51101completed2013-10-02T00:00:00Z
62116completed2013-10-02T00:00:00Z
73126completed2013-10-02T00:00:00Z
821212completed2013-10-03T00:00:00Z
931012completed2013-10-03T00:00:00Z
1041312cancelled_by_driver2013-10-03T00:00:00Z

Users table

users_idbannedrole
1Noclient
2Yesclient
3Noclient
4Noclient
10Nodriver
11Nodriver
12Nodriver
13Nodriver

Solution - Group By Approach

  • Use INNER JOIN to filter out trips involving a banned client or driver
  • Use WHERE clause to filter out trips outside the desired date range
  • Group request count by date and status
  • Use CASE statement with SUM() function to aggregate daily cancelled requests
  • Calculate cancellation rate for each day and round it to 2 decimal points
WITH trips_analysis
AS (
SELECT t.request_at,
t.STATUS,
count(t.id) AS requests
FROM Trips t
INNER JOIN Users AS client ON t.client_id = client.users_id
AND client.banned = 'No'
INNER JOIN Users AS driver ON t.driver_id = driver.users_id
AND driver.banned = 'No'
WHERE t.request_at BETWEEN '2013-10-01'
AND '2013-10-03'
GROUP BY t.request_at,
t.STATUS
)
SELECT request_at AS Day,
round(sum(CASE
WHEN STATUS LIKE 'cancelled%'
THEN requests
ELSE 0
END) / sum(requests), 2) AS "Cancellation Rate"
FROM trips_analysis
GROUP BY request_at;

This SQL query is using a Common Table Expression (CTE) to create a temporary table called trips_analysis that selects data from the Trips table and joins it with the Users table twice to get information about clients and drivers. The WHERE clause filters the results to only include data for a specific date range, and the GROUP BY clause aggregates the data by the request date and status of the trip.

The second part of the SQL query uses the trips_analysis CTE to calculate the cancellation rate for each day within the date range. The ROUND function is used to round the result to two decimal places, and the CASE statement within the SUM function is used to count the number of cancelled requests and divide it by the total number of requests for each day. Finally, the results are grouped by the request date to show the cancellation rate for each day.

Query Output

DayCancellation Rate
2013-10-01 00:00:000.33
2013-10-02 00:00:000.00
2013-10-03 00:00:000.50