Skip to content

Commit f5ae78e

Browse files
committed
task: #262
1 parent 7e55cc3 commit f5ae78e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
192192
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
193193
3. [Hard](./leetcode/hard/)
194194
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
195+
- [262. Trips and Users](./leetcode/hard/262.%20Trips%20and%20Users.sql)
195196
- [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql)
196197
- [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
197198
- [3451. Find Invalid IP Addresses](./leetcode/hard/3451.%20Find%20Invalid%20IP%20Addresses.sql)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Question 262. Trips and Users
3+
Link: https://leetcode.com/problems/trips-and-users/description/?envType=problem-list-v2&envId=database
4+
5+
Table: Trips
6+
7+
+-------------+----------+
8+
| Column Name | Type |
9+
+-------------+----------+
10+
| id | int |
11+
| client_id | int |
12+
| driver_id | int |
13+
| city_id | int |
14+
| status | enum |
15+
| request_at | varchar |
16+
+-------------+----------+
17+
id is the primary key (column with unique values) for this table.
18+
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.
19+
Status is an ENUM (category) type of ('completed', 'cancelled_by_driver', 'cancelled_by_client').
20+
21+
Table: Users
22+
23+
+-------------+----------+
24+
| Column Name | Type |
25+
+-------------+----------+
26+
| users_id | int |
27+
| banned | enum |
28+
| role | enum |
29+
+-------------+----------+
30+
users_id is the primary key (column with unique values) for this table.
31+
The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
32+
banned is an ENUM (category) type of ('Yes', 'No').
33+
34+
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.
35+
36+
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.
37+
38+
Return the result table in any order.
39+
*/
40+
41+
SELECT
42+
t.request_at AS "Day",
43+
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
44+
FROM Trips AS t
45+
LEFT JOIN
46+
Users AS u1
47+
ON t.client_id = u1.users_id
48+
LEFT JOIN
49+
Users AS u2
50+
ON t.driver_id = u2.users_id
51+
WHERE u1.banned != 'Yes' AND u2.banned != 'Yes' AND t.request_at BETWEEN '2013-10-01' AND '2013-10-03'
52+
GROUP BY t.request_at

0 commit comments

Comments
 (0)