From afba6c35c3f6bcbef1da8e5d6dfa0fb54f2bdcd7 Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Sat, 26 Jul 2025 21:29:52 +0300 Subject: [PATCH] task: #262 --- README.md | 1 + leetcode/hard/262. Trips and Users.sql | 52 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 leetcode/hard/262. Trips and Users.sql diff --git a/README.md b/README.md index 810d45d..437c301 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/leetcode/hard/262. Trips and Users.sql b/leetcode/hard/262. Trips and Users.sql new file mode 100644 index 0000000..246c9b3 --- /dev/null +++ b/leetcode/hard/262. Trips and Users.sql @@ -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