Similar Problems

Similar Problems not available

Trips And Users - Leetcode Solution

Companies:

  • amazon

LeetCode:  Trips And Users Leetcode Solution

Difficulty: Hard

Topics: database  

Problem Description: Trips And Users is a problem in which we have to analyze the data of taxi trips made by users and find out the cancellation rate of trips made by each driver.

There are three tables given in this problem:

  1. Trips table with columns (Id, Client_Id, Driver_Id, City_Id, Status, Request_at)

  2. Users table with columns (Users_Id, Banned, Role)

  3. Users table with columns (Users_Id, Banned, Role)

We have to calculate the cancellation rate of each driver. In order to solve this problem, we have to join three tables using suitable joins and group the data by driver_id and then calculate the cancellation rate of each driver.

Solution: Let's start by analyzing the problem and breaking it down into smaller parts.

  1. Joining the Trips, Users, and Users tables

First, we need to join Trips table with Users table (twice), where we can use the Join on clause to check if the User_Id in Trips table matches with the User_Id in Users table and the role of that user should be a "driver". Similarly, we will join the Users table with Trips table, and we need to check if the User_Id in Trips table matches with the Users_Id in Users table, and the role of that user should not be "banned".

The query for joining the tables is as follows:

SELECT t.Id, t.Client_Id, t.Driver_Id, t.Status, t.Request_at, u1.Role AS driver_role, u2.Banned
FROM Trips t
JOIN Users u1 ON t.Driver_Id = u1.Users_Id AND u1.Role = 'driver'
JOIN Users u2 ON t.Client_Id = u2.Users_Id AND u2.Banned = 'No';
  1. Grouping the data by Driver_Id

Once we have joined the tables, we need to group the data by driver_id. We can use the GROUP BY clause to group the data by driver_id.

SELECT t.Driver_Id, COUNT(*) AS total_trips, SUM(CASE WHEN t.Status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled_trips
FROM (
  SELECT t.Id, t.Client_Id, t.Driver_Id, t.Status, t.Request_at, u1.Role AS driver_role, u2.Banned
  FROM Trips t
  JOIN Users u1 ON t.Driver_Id = u1.Users_Id AND u1.Role = 'driver'
  JOIN Users u2 ON t.Client_Id = u2.Users_Id AND u2.Banned = 'No'
) t
GROUP BY t.Driver_Id;
  1. Calculating cancellation rate of each driver

Finally, we need to calculate the cancellation rate of each driver. We can use the cancelled_trips and total_trips obtained in the previous query to calculate the cancellation rate of each driver.

SELECT driver_id, IFNULL(cancelled_trips, 0) / total_trips AS cancellation_rate
FROM (
  SELECT t.Driver_Id AS driver_id, COUNT(*) AS total_trips, SUM(CASE WHEN t.Status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled_trips
  FROM (
    SELECT t.Id, t.Client_Id, t.Driver_Id, t.Status, t.Request_at, u1.Role AS driver_role, u2.Banned
    FROM Trips t
    JOIN Users u1 ON t.Driver_Id = u1.Users_Id AND u1.Role = 'driver'
    JOIN Users u2 ON t.Client_Id = u2.Users_Id AND u2.Banned = 'No'
  ) t
  GROUP BY t.Driver_Id
) t2;

This query will give us the output of the cancellation rate of each driver. The final output looks like:

+-----------+-----------------+
| driver_id | cancellation_rate |
+-----------+-----------------+
| 10        | 0.0000          |
| 1         | 0.5000          |
| 2         | 0.0000          |
| 3         | 0.3333          |
+-----------+-----------------+

Conclusion: In this problem, we learned about joining tables, grouping data, and calculating the rate. We used the above query to find the cancellation rate of each driver for the given taxi trips data.

Trips And Users Solution Code

1