Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
3. [Hard](./leetcode/hard/)
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
- [262. Trips and Users](./leetcode/hard/262.%20Trips%20and%20Users.sql)
- [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql)
- [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
- [3451. Find Invalid IP Addresses](./leetcode/hard/3451.%20Find%20Invalid%20IP%20Addresses.sql)
Expand Down
52 changes: 52 additions & 0 deletions leetcode/hard/262. Trips and Users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Question 262. Trips and Users
Link: https://leetcode.com/problems/trips-and-users/description/?envType=problem-list-v2&envId=database

Table: Trips

+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| client_id | int |
| driver_id | int |
| city_id | int |
| status | enum |
| request_at | varchar |
+-------------+----------+
id is the primary key (column with unique values) for this table.
The table holds all taxi trips. Each trip has a unique id, while client_id and driver_id are foreign keys to the users_id at the Users table.
Status is an ENUM (category) type of ('completed', 'cancelled_by_driver', 'cancelled_by_client').

Table: Users

+-------------+----------+
| Column Name | Type |
+-------------+----------+
| users_id | int |
| banned | enum |
| role | enum |
+-------------+----------+
users_id is the primary key (column with unique values) for this table.
The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
banned is an ENUM (category) type of ('Yes', 'No').

The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.

Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03" with at least one trip. Round Cancellation Rate to two decimal points.

Return the result table in any order.
*/

SELECT
t.request_at AS "Day",
ROUND(COUNT(CASE WHEN t.status = 'cancelled_by_driver' OR t.status = 'cancelled_by_client' THEN 1 END) / COUNT(t.id)::numeric, 2) AS "Cancellation Rate" --noqa: RF05
FROM Trips AS t
LEFT JOIN
Users AS u1
ON t.client_id = u1.users_id
LEFT JOIN
Users AS u2
ON t.driver_id = u2.users_id
WHERE u1.banned != 'Yes' AND u2.banned != 'Yes' AND t.request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY t.request_at